[Melbourne-pm] Simple(?) question involving backticks and die
Tim Connors
tconnors+pmmelb at astro.swin.edu.au
Wed Jan 4 21:38:28 PST 2006
On Thu, 5 Jan 2006, Benji Wakely wrote:
>
> On Thu, 5 Jan 2006 16:27:29 +1100 (EST),
> Tim Connors <tconnors+pmmelb at astro.swin.edu.au> wrote:
>
> >> my @list = `/bin/ls /home` || die ("Couldn't list files");
> >
> >Aren't you meant to use "or" rather than "||" when using die, for
> >precedence reasons? Could this be what is happening? `` becomes scalar
> >context when combined with the || ?
>
> That checks out, and now I'll happily use 'or' rather than '||'
> in this situation.
>
> ...Wish I knew why it was so, though.
Why?
Because what you were trying to do was to || the backticks with the die,
and then assigning the result into the array. || forces the backticks to
become a scalar, and the result is true, so die doesn't evaluate to
anything. Hence, the scalar ends up in the first element of the array.
The alternatives, are to bracket things correctly, forcing the evaluation
of the backticks in array context, then assigning that to the array, and
then evaluating whether to die. Or use "or", which has a lower precedence
than "||", as it is meant to be used -- the proper idiom is to use:
$blah=blah() or die()
rather than
$blah=blah() || die()
Read up on precedence in a perl or C book somewhere.
--
TimC
Disclaimer: Contents of this email are to be considered secret. You must
not read this email, and you must dispose of your computer immediately,
should you receive this email accidentally. I cannot take responsibility
for any incorrect information that this email may contain, especially the
bit about the monkeys and John Howard. Should anyone say otherwise, they
are obviously lying.
More information about the Melbourne-pm
mailing list