You have the right idea but your syntax is off.
The []s create a refrenence to an
array (when they don't extract elements from one),
so putting those around the split causes problems.
If you care about the
directive part, then
I'd think it would be better to split
once and use:
my ($command, $first, $second) = (split)[0,1,-1];
| [reply] [d/l] [select] |
Yes, you are right about the extra brackets. But the rest is more
complicated.
my ($command, $first, $second) = (split) [0, 1, -1];
$second = $first unless defined $second;
will only "work" because of a bug in Perl. It's still present in
perl5.6.1, however, it's fixed in the development release. Index -1
should always be the last element. Regardless of
how many elements there are in the list. The bug, however, is that
when indexing over the result of a split, you cannot
"reuse" elements. Repeating indexes result in undef.
And when there are only two elements in the split, 1
and -1 will point to the same element - making
$second undefined.
With the development version of Perl, you could just do:
my ($command, $first, $second) = (split) [0, 1, -1];
and $second will be the right thing: the last element
of the split. Hence, if there are just two items, $second
will be the same as $first, just what you want.
Luckely, the line
$second = $first unless defined $second;
won't harm you in the development version, as $second
will be defined, so no assignment is done.
-- Abigail
| [reply] [d/l] [select] |
| [reply] |
$second ||= $first;
Your code above allows for $second to be 0 (zero) and is perfectly readable. This (||=) way will not test for defined but for a true value so if $second is not allowed to be 0 (zero) then this is ideal (and more succinct). Otherwise, leave it how it is.
Hope this helps, larryk
* now that I'm not first to reply!
"Argument is futile - you will be ignorralated!" | [reply] [d/l] [select] |