Spurious "Use of uninitialized value" messages with '-w'

S

Sam Denton

Here's my code:

+208 chomp ($fsmount);
+209 $fsmount .= ' ';
+210
+211 print "*** fsmount = \"$fsmount\"";
+212 print ", empty" if $fsmount eq "";
+213 print ", undef" if undef $fsmount;
+214 print " ***\n";
+215
+216 # if ( $fsmount =~ m=^/= ) {
+217 # $fsmount =~ s/:.*$//;
+218 my $firstchar = substr($fsmount,0,1);
+219 if ( $firstchar eq '/' ) {
+220 $fsmount =~ s/:.*//;

When I run this code with '-w', here's what I see:

*** fsmount = " " ***
Use of uninitialized value in substr at unix_replica.pl line 218.
Use of uninitialized value in pattern match (m//) at unix_replica.pl line 219.
*** fsmount = "/: " ***
Use of uninitialized value in substr at unix_replica.pl line 218.
Use of uninitialized value in pattern match (m//) at unix_replica.pl line 219.

Everything works just fine without the '-w' option. Any ideas?
 
L

Lukas Mai

Sam Denton schrob:
Here's my code:
+208 chomp ($fsmount);
+209 $fsmount .= ' ';
+210
+211 print "*** fsmount = \"$fsmount\"";
+212 print ", empty" if $fsmount eq "";
+213 print ", undef" if undef $fsmount;
^^^^^^^^^^^^^^
$ perldoc -f undef
undef EXPR
undef Undefines the value of EXPR, which must be an
lvalue. [...]
+214 print " ***\n";
+215
+216 # if ( $fsmount =~ m=^/= ) {
+217 # $fsmount =~ s/:.*$//;
+218 my $firstchar = substr($fsmount,0,1);
^^^^^^^^
$fsmount is undef here, that's why you get the warning.

[...]
*** fsmount = " " ***
Use of uninitialized value in substr at unix_replica.pl line 218.
[...]

HTH, Lukas
 
M

Michele Dondi

Everything works just fine without the '-w' option. Any ideas?

(1) 'use warnings;' instead (unless backward compatibility is an
issue), (2) if you're really sure that what's going on is correct
anyway, then wrap a small block around the relevant line(s) of code
and set

no warnings 'uninitialized';

there.


HTH,
Michele
 
U

Uri Guttman

SD> +211 print "*** fsmount = \"$fsmount\"";
SD> +212 print ", empty" if $fsmount eq "";
SD> +213 print ", undef" if undef $fsmount;

that should be defined and not undef. also do that before the other
print. actually you should do only one or the other since it can't be
both '' and undef. and you are also printing $fsmount before this so if
it is undef you will get the warning. it is not spurious, heed it.

SD> Everything works just fine without the '-w' option. Any ideas?

you have undefined values lurking around. fix them.

uri
 
T

Tad McClellan

Sam Denton said:
Subject: Spurious "Use of uninitialized value" messages with '-w'


There are no "spurious" messages.

"spurious" does not mean "I don't understand where they come from",
it means unwarranted messages.

You do indeed use an undef value (that's what makes "uninitialized value"
messages), so the warnings _are_ warranted.

+213 print ", undef" if undef $fsmount;


You just told perl to give $fsmount the undef value.

Why did you tell it that?

I think you wanted this instead:

print ", undef" unless defined $fsmount;
 
A

Arndt Jonasson

Uri Guttman said:
SD> +213 print ", undef" if undef $fsmount;

that should be defined and not undef. also do that before the other
print. actually you should do only one or the other since it can't be
both '' and undef. and you are also printing $fsmount before this so if
it is undef you will get the warning. it is not spurious, heed it.

Could a case be made for having perl emitting a warning with something
like
line 213: 'if' used with always false conditional
when compiling?

(I only checked on 5.005; maybe 5.8 has this already.)
 
A

Anno Siegel

Arndt Jonasson said:
Could a case be made for having perl emitting a warning with something
like
line 213: 'if' used with always false conditional
when compiling?

Unlikely. It's a common technique to rely on the optimizer to discard
code that will never be executed, as in

use constant DEBUG => 0;
# ...
if ( DEBUG ) {
# cod here won't even compile unless DEBUG is set
}

If this warned, a lot of old code would break.

Anno
 

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

Forum statistics

Threads
474,269
Messages
2,571,100
Members
48,773
Latest member
Kaybee

Latest Threads

Top