[Zope-Perl] Next question

Gisle Aas gisle@ActiveState.com
03 Aug 2000 12:42:50 +0200


Monty Taylor <mtaylor@goldridge.net> writes:

> How do I access self? (Not meta-physically, but in the sense of the
> calling object :) )

You put the string "self" into the Arguments field.  The Arguments
field is a comma separated list of words.  Example:

  Arguments:  self, foo, RESPONSE

will make ZPublisher call the perl method with @_ containing 3 values.
The first and third being references to some objects, and the second
picked up for the query string or something.

> I'm trying to get at the RESPONSE object to set some headers. Any ideas?

This example works for me:

  Arguments: RESPONSE
  Code:      my $res = shift;
             # This is a comment
             $res->setHeader("Copyright" => "2000 Foo");
             "2\n";

If I then try to access the corresponding URL with LWP it looks like this:

$ GET -e http://eik:8080/sub/foo3
Connection: close
Date: Thu, 03 Aug 2000 10:35:19 GMT
Server: Zope/(unreleased version) ZServer/1.1b1
Content-Length: 2
Content-Type: text/plain
Client-Date: Thu, 03 Aug 2000 10:35:19 GMT
Client-Peer: 194.143.13.62:8080
Copyright: 2000 Foo

2

> Also, comments don't seem to work so well in PerlMethods. I commented
> out some lines and then got syntax errors. I took out the comment lines
> and all was good again.

Hmm.  It works for me.  Perhaps an issue with what kind of line
endings your browser produce.

> I have succeeded in using LWP::UserAgent to serve third-party content
> from Zope... mirroring the How-To of the same nature that tells you how
> to use ZPublisher.Client. As far as I could tell, though,
> ZPublisher.Client didn't have proxy or cookie handling support. Anyway,
> for what it's worth, here's the code I knocked up to do it.
> 
> DTMLDocument: dilbert.gif
>   Properties: content_type=image/gif
>   Content:
> <dtml-var dilbert>
> 
> PerlMethod: dilbert
>   Content:
> use LWP::UserAgent;
> 
> my $ua = LWP::UserAgent->new;
> my $request = HTTP::Request->new('GET','http://www.dilbert.com');
> $ua->proxy(['http','ftp'], 'http://192.168.131.1:8080');
> my $response = $ua->request($request);
> my $content = $response->content;
> $content =~ m,(/comics/dilbert/archive/images/[^"]*),s;
> my $img = HTTP::Request->new('GET',"http://www.dilbert.com/$1");
> my $img_response = $ua->request($img);
> $content = $img_response->content;
> return $content;
> 
> Then, for fun, I added:
> <img src="dilbert.gif></img> 
> to my standard_html_footer.
> Works like a charm!

Cool.

Regards,
Gisle