[Petal] Re: BUG: Petal & Autoload (Was var:object/method fails on autoloaded methods)

Kurt Stephens kstep@pepsdesign.com
Sun, 1 Sep 2002 15:16:36 -0400


> > I was playing around with using Petal for form processing using CGI.pm,
and
> > noticed that expressions such as "query/param 'FirstName'" work fine
while
> > "query/textfield 'FirstName'" fail.  After looking through CGI.pm, I
> > realized that the param method is compiled when the module loads,
however
> > the HTML generating methods are autoloaded on demand.  I was able to
correct
> > this in the case of CGI.pm using the -compile option, however this will
not
> > work for other packages using autoloaded methods that do not provide a
means
> > for overriding the autoload behavior.
>
> Interesting... The reason for this behavior is that Petal is trying to
> see if a method exists before invoking it via can(). I didn't know that
> can() did return FALSE if the calls were catched using AUTOLOAD.
>
> I consider this as a bug, hence it'll be fixed in the next Petal release
> (with a couple of other nasty bugs which have been reported since) which
> should be in mid-end october.
>

It looks like the bug is in the interaction between Perl's autoload
mechanism
and UNIVERSAL::can().  I re-ran my code using CGI3.pm without the
-compile switch, and the problem magically went away.  CGI3::Object
explicitly
typeglobs all of its autoloaded methods, so I suspect that this is a
workaround
for a known problem.

i.e.

*a = \&CGI3::Object::Html::a;
*Accept = \&CGI3::Object::Request::Accept;
*address = \&CGI3::Object::Html::address;
*append = \&CGI3::Object::Misc::append;

...

>
> > By the way, calling the HTML generating methods of CGI.pm from the Petal
> > template provides an extremely simple means to develop forms.  For
example,
> > the ususal ratnest of loops used to populate a checkbox group can be
> > replaced by the simple elegant construct below.  I left in a dummy
checkbox
> > to give the HTML designer something to look at.
> >
> > <span petal:replace="query/checkbox_group 'Choices' choices '' 'true'">
> > <input name="Choices" type="checkbox" value="test">Test</input>
> > </span>
>
> Thanks for the trick, I was not aware of it. However, I do not like the
> fact that CGI.pm encapsulates such HTML widgets. Maybe developing an
> HTML form widget adapted to Petal (and its syntax) could be an
> interesting idea...
>

I agree that CGI.pm has a lot of overhead for this sort of thing, plus the
fact that
using code to generate HTML from within the template goes against the whole
philosophy behind Petal.  I would be very interested in working on a
framework
that would integrate persistent forms with the Petal syntax.  I will post
suggestions
to the list once I've had some more time to think things over.

> BTW, you could have saved a span tag using the following:
>
> <input
>  name="Choices"
>  type="checkbox"
>  value="test"
>  petal:replace="query/checkbox_group 'Choices' choices ''
'true'">Test</input>
>

Thanks for the tip!  I should have spotted that one myself.

Best Regards,

Kurt Stephens
kstep@pepsdesign.com