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

Discussion in 'Perl Misc' started by Sam Denton, Nov 4, 2004.

  1. Sam Denton

    Sam Denton Guest

    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?
    Sam Denton, Nov 4, 2004
    #1
    1. Advertising

  2. Sam Denton

    Lukas Mai Guest

    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,
    Lukas Mai, Nov 4, 2004
    #2
    1. Advertising

  3. On 4 Nov 2004 15:03:49 -0800, (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,
    Michele Dondi, Nov 4, 2004
    #3
  4. Sam Denton

    Uri Guttman Guest

    >>>>> "SD" == Sam Denton <> 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
    Uri Guttman, Nov 4, 2004
    #4
  5. Sam Denton <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Nov 5, 2004
    #5
  6. Uri Guttman <> writes:
    > >>>>> "SD" == Sam Denton <> 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.)
    Arndt Jonasson, Nov 5, 2004
    #6
  7. Sam Denton

    Anno Siegel Guest

    Arndt Jonasson <> wrote in comp.lang.perl.misc:
    >
    > Uri Guttman <> writes:
    > > >>>>> "SD" == Sam Denton <> 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
    Anno Siegel, Nov 5, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. G Kannan
    Replies:
    1
    Views:
    1,223
    Eric J. Roode
    Oct 11, 2003
  2. Sukhbir Dhillon
    Replies:
    1
    Views:
    6,236
    Joe Smith
    Apr 5, 2004
  3. smartins68
    Replies:
    1
    Views:
    5,990
    Joe Smith
    Jun 9, 2004
  4. Liang
    Replies:
    2
    Views:
    751
    Liang
    Aug 11, 2004
  5. Sam Denton
    Replies:
    10
    Views:
    142
    Ben Morrow
    Nov 6, 2004
Loading...

Share This Page