Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Use of uninitialised value, how to avoid?

Reply
Thread Tools

Use of uninitialised value, how to avoid?

 
 
Justin C
Guest
Posts: n/a
 
      07-22-2010
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.
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      07-22-2010
Justin C <(E-Mail Removed)> 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 ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      07-22-2010
On Thu, 22 Jul 2010 14:34:08 +0100, Justin C <(E-Mail Removed)> 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
 
Reply With Quote
 
Wolf Behrenhoff
Guest
Posts: n/a
 
      07-22-2010
On 22.07.2010 16:20, Jens Thoms Toerring wrote:
> Justin C <(E-Mail Removed)> 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

 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      07-22-2010
On Thu, 22 Jul 2010 08:03:25 -0700, (E-Mail Removed) wrote:

>On Thu, 22 Jul 2010 14:34:08 +0100, Justin C <(E-Mail Removed)> 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

 
Reply With Quote
 
Justin C
Guest
Posts: n/a
 
      07-22-2010
On 2010-07-22, Justin C <(E-Mail Removed)> 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.
 
Reply With Quote
 
Justin C
Guest
Posts: n/a
 
      07-23-2010
On 2010-07-22, (E-Mail Removed) <(E-Mail Removed)> wrote:
> On Thu, 22 Jul 2010 14:34:08 +0100, Justin C <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
use of uninitialised value in pattern match (m//) Jess Perl Misc 2 05-17-2006 06:08 AM
value of an uninitialised variable Andy Fish XML 7 01-10-2005 04:37 PM
use of uninitialised value Cognition Peon Perl Misc 5 04-15-2004 08:29 AM
C++: Uninitialised Variable Passed as a Parm Gene Wirchenko C++ 22 12-19-2003 02:26 PM



Advertisments