Use of uninitialised value, how to avoid?

J

Justin C

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.
 
J

Jens Thoms Toerring

Justin C said:
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
 
S

sln

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
 
W

Wolf Behrenhoff

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
 
S

sln

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
 
J

Justin C

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.
 
J

Justin C

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top