[Zope-Perl] REQUEST object

Gisle Aas gisle@ActiveState.com
02 Aug 2000 16:15:25 +0200


Monty Taylor <mtaylor@goldridge.net> writes:

> Gisle Aas wrote:
> > The REQUEST object will just be Python::Object reference when passed
> > to perl.  The reason you see the contents as html is probably that the
> > python object underneath provide a __str__ method returning that stuff
> > and if you simply use a Python::Object in string context perl will
> > invoke the equivalent of str(o) and use that.  If you just call
> > methods on the object it should work.
> 
> That makes sense. I was calling it in string context. 
> 
> Here's what I finally did that worked:
> dtml-method:
> <dtml-call "test_perl(REQUEST)">
> 
> PerlMethod:
>   Arguments: REQUEST
>   Code:      my $foo=shift;
>              foreach ( $foo->keys ){
>                print;  #Goes to stderr in Debug mode
>                print "\n";
>              }
> 

> As a note, test_perl(REQUEST=REQUEST) does not work, and neither
> does just plain test_perl().

I am not totally sure what test_perl(REQUEST=REQUEST) does in DTML,
but if it was python and test_perl was a perl function then it would
invoke the function as:

   test_perl("REQUEST", REQUEST);

i.e. you get two arguments where the second is the object you want.
Python keyword arguments are passed as key/value pairs to a perl
function after any position arguments.  I could not figure out a
better way to handle them.

And test_perl() probably calls it directly without any arguments at
all so $foo would be 'undef' in this case.

> What does the REQUEST in the arguments get us?

It is for ZPublisher's mashalling of various HTTP variables into a
function argument list.  If you access your perl method directly as:

   http://yourzope:8080/test_perl

then ZPublisher will call the function with the arguments you
specified in the "Arguments" field.

> Since we're using Perl shift to grab arguments from @_,
> we're not really providing named parameters.

Correct.  Perl does not have named routine parameters yet.

> Yet without REQUEST in the arguments, it doesn't work.

Again, the arguments only apply if you publish the method directly.
If you embed a direct call from DTML then you need to provide whatever
methods are needed, but the perl runtime does not check the argument
list for you as python would have done.  You don't get an exception
unless the perl code explicitly checks it's arguments and die on
trouble.

> This method of doing things seems to work fine, but I'd love to
> understand what's actually happening here. Any thoughts?

I hope things are clearer now.  Please continue to ask if things are
still unclear.

Regards,
Gisle