[Petal] compiled expression (again)

Fergal Daly fergal at esatclear.ie
Wed Jun 18 16:34:12 BST 2003


On Wednesday 18 June 2003 2:03 pm, William McKee wrote:
> >>    Your templates may now generate "undefined value" warnings if you try 
to use
> >>    an undefined value. Previously, Petal prevented many of these from
> >>    occurring. As always, the best thing to do is not to avoid using 
undefined
> >>    values in your templates.
> 
> Does this mean that I can no longer have undefined hash entries if I use
> the element in the template? Hmm, I prefer the way Petal does it or at
> least the ability to make a choice as HTML::Template offers. Is this a
> technical limitation or a design decision?

It's a decision by me but it wouldn't be a big deal to wrap all accesses in 
some code to prevent this happening.

Petal currently generates code like this

$res .= $hash->get( "user" );

get() translates undef to "", thus avoiding the warning.

The new one generates this

$res .= $hash->{"user"};

so if $hash->{"user"} is undefined then we get the warning.

Easy solutions are to to generate code like this

$res .= do{ my $value = $hash->{"user"}; defined $value ? $value : "" };

or

$res .= define($hash->{"user"});

where define() is a function that replaces undef with "".

Either of these would be very easy to do but obviously there is a speed 
penalty.

A better solution may be to do something like turn off some warnings in your 
template. Something like

no warnings 'uninitialized';

I think it should be configurable, defaulting to not warning but the test 
suite should definitely have all warnings turned on.

I just tried to make a patch for that and discovered a problem. When Petal is 
executing the template in a safe compartment, it's not allowed do a require 
(which is basically what a "no" is). I tried changing to

warnings->unimport('uninitialized')

but that doesn't turn them off. As far as I can tell, that's a bug in Perl 
I'll investigate that further.

A slightly worse way to do it is

local $^W = 0;

at the top of your templates.

This hides all warnings from all code until the template is finished 
processing.

Whether this is preferrable to allowing requires inside the safe compartment, 
I don't know.

I've attached a patch using the $^W method, it also has to turn off warnings 
in XML_Encode_Decode. Applying this patch eliminates all those warnings by 
default. If you want the warnings just do

$Petal::WARN_UNINIT = 1;

F
-------------- next part --------------
A non-text attachment was scrubbed...
Name: warn.patch
Type: text/x-diff
Size: 1031 bytes
Desc: not available
Url : http://www.email-lists.org/pipermail/petal/attachments/20030618/ad4248d9/warn.bin


More information about the Petal mailing list