Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Spurious "Use of uninitialized value" messages with '-w'

Reply
Thread Tools

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

 
 
Sam Denton
Guest
Posts: n/a
 
      11-04-2004
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?
 
Reply With Quote
 
 
 
 
Lukas Mai
Guest
Posts: n/a
 
      11-04-2004
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
--
use warnings; use strict;
sub hacker'Perl { "Perl @_,"} sub another'Just
{print"Just @_ "}
Just another Perl hacker,
 
Reply With Quote
 
 
 
 
Michele Dondi
Guest
Posts: n/a
 
      11-04-2004
On 4 Nov 2004 15:03:49 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) (Sam Denton) wrote:

>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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      11-04-2004
>>>>> "SD" == Sam Denton <(E-Mail Removed)> writes:

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
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-05-2004
Sam Denton <(E-Mail Removed)> wrote:

> 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;



--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Arndt Jonasson
Guest
Posts: n/a
 
      11-05-2004

Uri Guttman <(E-Mail Removed)> writes:
> >>>>> "SD" == Sam Denton <(E-Mail Removed)> writes:

>
> 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.)
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      11-05-2004
Arndt Jonasson <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> Uri Guttman <(E-Mail Removed)> writes:
> > >>>>> "SD" == Sam Denton <(E-Mail Removed)> writes:

> >
> > 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?


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
 
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
spurious DMZ Barrett Bonden Cisco 1 02-01-2006 05:50 AM
Status query: Java 1.5 Linux spurious mouse clicked events. Ian A. Mason Java 0 02-24-2005 04:15 AM
Re: spurious wakeup Markus Elfring C++ 0 11-25-2004 11:34 AM
Spurious "Use of uninitialized value" with -w, redux Sam Denton Perl Misc 10 11-06-2004 03:56 PM
Spurious Access Mark St Laurent Cisco 6 10-01-2004 02:20 AM



Advertisments