Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Complaints from Warnings about uninitialized var

by freddo411 (Chaplain)
on Mar 10, 2005 at 00:42 UTC ( [id://438113]=perlquestion: print w/replies, xml ) Need Help??

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

Consider this code:
#!/usr/local/bin/perl -w use strict; my $last = ""; my $first = ""; my $middle = ""; ( $last, $first ) = split /,/ , 'thumb,tom' ; print "last:($last) first($first) middle($middle)\n"; ( $first, $middle ) = split / / , $first ; print "last:($last) first($first) middle($middle)\n";
And what it outputs:
gonzo:/export/home/fredk/: ./bar.pl last:(thumb) first(tom) middle() Use of uninitialized value at ./bar.pl line 11. last:(thumb) first(tom) middle()

Does it make sense that warnings would complain about $middle not being initialized? In my case (the above is simplified) there are some records where there is <NULL> for $middle and others where $middle is defined. So when I print $middle and it is <NULL>, that's WIM (What I Mean).

Suggested work arounds?

-------------------------------------
Nothing is too wonderful to be true
-- Michael Faraday

Replies are listed 'Best First'.
Re: Complaints from Warnings about uninitialized var
by Fletch (Bishop) on Mar 10, 2005 at 00:56 UTC

    It's not <NULL>, it's undef because you've attempted to assign a one element list (the result of your second split) to a list of two variables. This results in undef getting stuck in the second (and if there were more, any subsequent) variable. Even though you've initialized it above, you're blowing that value away. What you probably want to do is to check if $middle is defined after you've split (e.g. $middle = "" unless defined $middle;).

Re: Complaints from Warnings about uninitialized var
by Tanktalus (Canon) on Mar 10, 2005 at 01:00 UTC

    It makes sense - because it gets to tell you that middle didn't exist. Try:

    ( $first, $middle ) = split ' ', "$first ";
    split will now say that there was a "middle" field, and split it out. It will be empty, and you'll get what you want. Another alternative is, after splitting, just say:
    $middle ||= '';
    Unfortunately, if $middle were 0, you'd be resetting it to blank. So you could do:
    $middle //= '';
    Woops - that's perl 6 (or perl 5.10, I hear). I meant:
    $middle = '' unless defined $middle;
    Same thing as the previous one, except it works now in perl 5.

    Without this undef-edness, there'd be no way to tell the difference between a field existing and not. Perl is telling you the field simply doesn't exist. You can fake it to make sure the field exists by putting a space there, or you can deal with the lack of field by resetting it to blank when it is returned as undef. Either way, you're being explicit about what you want, so that no one is confused.

Re: Complaints from Warnings about uninitialized var
by mifflin (Curate) on Mar 10, 2005 at 00:56 UTC
    after your split to get $middle you could do

    $middle ||= '';
    or
    $middle = '' unless defined $middle;
Re: Complaints from Warnings about uninitialized var
by jhourcle (Prior) on Mar 10, 2005 at 03:21 UTC

    If you do not want to modify the value of $middle (so you leave it as undef, and not an empty string), you may want to use printf:

    printf "last:(%s) first(%s) middle(%s)\n", $last || '', $first || '', $middle || '';

    or if you've got a whole lot to deal with

    printf "some format string goes here", map { defined($_) ? $_ : '' } (@list_of_variables);

    Or, there's always:

    { no warnings 'uninitialized'; print "whatever"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://438113]
Approved by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-28 17:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found