in reply to uninitialized variables

IF (and ONLY if) you can handle a default value ok, you could do something like this:
while(<>) { my ($x, $y, $z) = (0,0,0); ($x, $y, $z)=split; # do whatever }
Or you could
get silly, and do something like:
while(<>) { my @flds=split; my ($x, $y, $z)=(@flds,0,0,0); # do whatever }
But then you have to be ready to handle 0's (or whatever other default you chose) when data is missing, and you have to make sure you don't pick a default value that could be real data.

It's much safer to use defined and know when your fields are missing; that way you get something like real NULL handling, in the SQL sense.
--
Mike

Edit: thanks to ariels for the correction!

Replies are listed 'Best First'.
Re: Re: uninitialized variables
by ariels (Curate) on Mar 28, 2002 at 17:29 UTC

    There's a subtle difference between the 2 forms you suggest: only the second works!

    When you say

    my ($x, $y, $z) = (0,0,0); ($x, $y, $z)=split;
    you always set $x, $y and $z! That's what assignment is all about!

    Here it is in the debugger:

    main::(-e:1):   0
      DB<1> ($x,$y,$z)=(2,3,4)
    
      DB<2> ($x,$y,$z)=split ' ',"two words"
    
      DB<3> x $x
    0  'two'
      DB<4> x $y
    0  'words'
      DB<5> x $z
    0  undef
    

    The second works, because it isn't trying to reassign undef.