Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: What protects me from doing this stupid thing..?

by gam3 (Curate)
on Aug 12, 2007 at 11:58 UTC ( [id://632051]=note: print w/replies, xml ) Need Help??


in reply to What protects me from doing this stupid thing..?

It's not that pretty, but
$template->param( some_var => 10, (some_var => $hash{some_possibly_undefined_key}) x!! exists($hash{so +me_possibly_undefined_key}), );
will do what you want. Or
$template->param( some_var => exists $hash{some_possibly_undefined_key} ? $hash{some_p +ossibly_undefined_key} : 10; );
However, you might also try using Params::Validate
{ package MyTemplate; use base Template; use Params::Validate qw (validate); sub param { my $self = shift; my %p = validate(@_, { some_var => { default => 12, } }); $self->SUPER::param(%p); } }
To actually answer your question, some things are hard to protect against and it is best to just try to avoid some constructs. For example you can write
if ($a == 0) 
or you can write
if (0 == $a)
using the second will keep you from the problem of writing if ($a = 0).
-- gam3
A picture is worth a thousand words, but takes 200K.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://632051]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-23 19:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found