Prev: Speed of reading some MB of data using qx(...)
Next: Posting Guidelines for comp.lang.perl.misc ($Revision: 1.9 $)
From: Justin C on 22 Jul 2010 09:34 I'm trying to avoid warnings in my Apache logs. Here's the code that's causing the problem: unless ($methods->{$param{method}}) { $param{method} = "default"; } If the web-page in question is called without any parameters then $param{method} is undef, in which case I want $param->{method} = "default". When the page is called with parameters all is OK. What is a better way of doing the above to avoid "Use of uninitialized value $param{"method"}" ? Justin. -- Justin C, by the sea.
From: Jens Thoms Toerring on 22 Jul 2010 10:20 Justin C <justin.1007(a)purestblue.com> wrote: > I'm trying to avoid warnings in my Apache logs. Here's the code that's > causing the problem: > unless ($methods->{$param{method}}) { > $param{method} = "default"; > } > If the web-page in question is called without any parameters then > $param{method} is undef, in which case I want $param->{method} = > "default". > When the page is called with parameters all is OK. > What is a better way of doing the above to avoid "Use of uninitialized > value $param{"method"}" ? In the 'unless' condition you use $methods->{$param{method}} so if $param{method} is undefined you may be asking for a hash element with an undefined key. Shouldn't that line actually be unless ($param{method}) { $param{method} = "default"; } or just $param{method} = "default" unless $param{method}; if you want to check if $param{method} is defined and assign a default value otherwise? Regards, Jens -- \ Jens Thoms Toerring ___ jt(a)toerring.de \__________________________ http://toerring.de
From: sln on 22 Jul 2010 11:03 On Thu, 22 Jul 2010 14:34:08 +0100, Justin C <justin.1007(a)purestblue.com> wrote: >I'm trying to avoid warnings in my Apache logs. Here's the code that's >causing the problem: > >unless ($methods->{$param{method}}) { > $param{method} = "default"; >} > >If the web-page in question is called without any parameters then >$param{method} is undef, in which case I want $param->{method} = >"default". > >When the page is called with parameters all is OK. > >What is a better way of doing the above to avoid "Use of uninitialized >value $param{"method"}" ? > > Justin. Depends on what you want: # (undef,'') fail ; (0) pass unless (defined $param{method} and length $param{method}) { $param{method} = "default"; } # (undef,0,'') fail $param{method} ||= "default"; # (undef) fail ; (0,'') pass $param{method} //= "default"; -sln
From: Wolf Behrenhoff on 22 Jul 2010 11:04 On 22.07.2010 16:20, Jens Thoms Toerring wrote: > Justin C <justin.1007(a)purestblue.com> wrote: >> When the page is called with parameters all is OK. > >> What is a better way of doing the above to avoid "Use of uninitialized >> value $param{"method"}" ? > > In the 'unless' condition you use > > $methods->{$param{method}} > > so if $param{method} is undefined you may be asking for a hash > element with an undefined key. Shouldn't that line actually be > > unless ($param{method}) { > $param{method} = "default"; > } Probably this is not a solution because this removes the check whether there exists a key in %$methods. If you omit this check, the user could specify any method, also non existing ones. To get rid of the warning, one can use for example: unless ($param{'method'} && $methods->{$param{'method'}}) { ... } Something like $param{method} ||= "default"; die "invalid method\n" unless $methods->{$param{method}}; might be better than overwriting wrong methods with the default. But this of course depends. Wolf
From: sln on 22 Jul 2010 11:33
On Thu, 22 Jul 2010 08:03:25 -0700, sln(a)netherlands.com wrote: >On Thu, 22 Jul 2010 14:34:08 +0100, Justin C <justin.1007(a)purestblue.com> wrote: > >>I'm trying to avoid warnings in my Apache logs. Here's the code that's >>causing the problem: >> >>unless ($methods->{$param{method}}) { >> $param{method} = "default"; >>} >> >>If the web-page in question is called without any parameters then >>$param{method} is undef, in which case I want $param->{method} = >>"default". >> >>When the page is called with parameters all is OK. >> >>What is a better way of doing the above to avoid "Use of uninitialized >>value $param{"method"}" ? >> >> Justin. > >Depends on what you want: > ># (undef,'') fail ; (0) pass > unless (defined $param{method} and length $param{method}) { > $param{method} = "default"; > } ># (undef,0,'') fail > $param{method} ||= "default"; > ># (undef) fail ; (0,'') pass > $param{method} //= "default"; > So perhaps: use strict; use warnings; my %param; my $methods = { default => sub {print "Default handler method ..\n"}, } ; $param{method} //= "default" ; exists $methods->{ $param{method} } and $methods->{ $param{method} }() or die "Cannot find $param{method}() method" ; -sln |