Angharad has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Use of uninitialized value
by jasonk (Parson) on Feb 28, 2007 at 15:41 UTC

    It's likely to mean exactly what it says. From perldiag:

           Use of uninitialized value%s
               (W uninitialized) An undefined value was used as if it were already
               defined.  It was interpreted as a "" or a 0, but maybe it was a
               mistake.  To suppress this warning assign a defined value to your
               variables.
    
               To help you figure out what was undefined, perl tells you what
               operation you used the undefined value in.  Note, however, that
               perl optimizes your program and the operation displayed in the
               warning may not necessarily appear literally in your program.  For
               example, "that $foo" is usually optimized into ""that " . $foo",
               and the warning will refer to the "concatenation (.)" operator,
               even though there is no "." in your program.
    

    We're not surrounded, we're in a target-rich environment!
Re: Use of uninitialized value
by davorg (Chancellor) on Feb 28, 2007 at 15:44 UTC
Re: Use of uninitialized value
by andye (Curate) on Feb 28, 2007 at 15:57 UTC
    Angharad,

    A 'string' is a variable with some text in it.
    But yours doesn't have anything in it, it's empty.
    It's empty because you haven't put anything in it.
    On whatever line the error's coming up, Perl is expecting that there should be something in it.
    Perl is warning you that you could have made a mistake.

    Here's an example that produces the same error:

    use warnings; my $foo; if ($foo eq "hey!") { print "yes" }
    Here Perl complains about an uninitialised value, because I'm checking to see if $foo equals "yes" when I haven't put anything at all in $foo. If I started off by saying my $foo = 'no' or even  my $foo = '' then the warning wouldn't appear.

    Hope that helps.

    andye

    (by the way: sorry if any of this sounds patronising: your question was pretty short so I couldn't tell what you already knew, so I thought it better to start from first principles as much as I could. Hope that's OK).

Re: Use of uninitialized value
by johngg (Canon) on Feb 28, 2007 at 16:28 UTC
    These two snippets run from the command line show the difference between just declaring a scalar and initialising it to an empty string. Running under use warnings, the first snippet generates the message, the second doesn't as the string has been initialised.

    $ perl -e ' > use strict; > use warnings; > my $string; > print qq{>$string<\n};' Use of uninitialized value in concatenation (.) or string at -e line 5 +. >< $ perl -e ' > use strict; > use warnings; > my $string = q{}; > print qq{>$string<\n};' >< $

    I hope this illustrates the difference.

    Cheers,

    JohnGG

Re: Use of uninitialized value
by kyle (Abbot) on Feb 28, 2007 at 15:43 UTC
Re: Use of uninitialized value
by Angharad (Pilgrim) on Feb 28, 2007 at 16:24 UTC
    Thanks for your help so far.
    The offending line is something like this
    $item = "$code[0]" . "."."$code[1]" . ".". "$code[2]";
    Any comments appreciated

      Erm . . . ouch.

      • The quotes around "$code[0]" are superfluous, to start
      • You could have just written it as "$code[0].$code[1].$code[2]" in one swell foop
      • Of course that locks you in to a fixed number of elements; if you want to join an arbitrary number of items in @code then just use join: $item = join( ".", @code );
        If want a specific subset of elements, then you can use a slice:
        my $item = join( ".", @code[0..2] ); my $not_item = join( ".", @code[9,7,0,3] );


        TGI says moo

        Fletch, agreed that it's untidy as it is, but  $code[0].$code[1].$code[2] ne join( ".", @code ).

        Best,

        andye

        Update: Anno is right, I did overlook a pair of quotes. Ignore me. :)

      One or more of ($code[0], $code[1], $code[2]) has nothing in it.

      Best,

      andye

A reply falls below the community's threshold of quality. You may see it by logging in.