in reply to split and uninitialized variables

I'm sure there are a lot of ways to do that, but I'd probably go with
my ($x, $y, $z) = (split(',', $_), '' x 3);
which does the split, but also puts empty strings onto the end of the list. Then your variables grab the first three items from the list, running into the empty strings if they run out of data from the split.

Update: ambrus is right. That's what I get for testing code without warnings on.

Replies are listed 'Best First'.
Re^2: split and uninitialized variables
by ambrus (Abbot) on Sep 03, 2004 at 15:46 UTC

    This won't work, as '' x 3 is just ''. Instead,

    my ($x, $y, $z) = (split(',', $_), ('') x 3);
    will work.
Re^2: split and uninitialized variables
by markjugg (Curate) on Sep 03, 2004 at 15:38 UTC
    This solution is novel and works, but I don't recommend it.

    . It's more important to have code that is clear and easy to read than to be as concise and compact as possible. The clearest solution is to simply use a second line to initialize the variables.

      I'm aware of the importance of clarity. If I were golfing I would have done something like
      my ($x, $y, $z) = split(',', "$_,,");
      but this was the most natural answer to me. The problem, as I saw it, was "sometimes the list returned by split is too short, and I want empty strings to fill missing places at the end," so the most natural answer I saw was to add items to fill it out.