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 11:37 On 2010-07-22, 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"}" ? Thanks to Jens and Wolf. Between you both you spotted a flaw in my code and provided the work-around. And sln, I think the last two of your suggestions were 5.10 solutions, these are operators I've yet to explore, though I can see, from your explanation, what each is doing. I'll investigate them further. Justin. -- Justin C, by the sea.
From: Justin C on 23 Jul 2010 04:41
On 2010-07-22, sln(a)netherlands.com <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"; What I want is to test that $param{method} is a valid method (if it has been supplied at all). If it's not a valid method, or is not defined, then it is to be set to "default". I've gone with (line wrapped to avoid break): $param{method} = "default" unless ( ($param{method}) && ($methods->{$param{method}}) ); This avoids the "Use of uninitialized.." in my Apache logs. I'm trying to get my head around '||=' and '//=', they're great shortcuts, I've just got to use them a few times to get it set in my head. Justin. -- Justin C, by the sea. |