in reply to Re: question about || operator and list context
in thread question about || operator and list context

Since you were curious, here's the (fixed) real world code:

use File::Temp (qw/tempfile/); (($fh,$fname) = tempfile('quanta-XXXX')) || die "Couldn't create temp file $fname\n";

The results without the extra parens differed a bit from the example above, though. $fh got set to a GLOB (a filehandle, I assume). This is the /first/ element in the array return, not the last.

I understand why that is now, though-- the || forced the subroutine to scalar context, in which it just returns a filehandle.

This made it more mysterious to debug though, since it wasn't returning just the last element of the array, which I might have recognized sooner.

Thanks to all for your help.

Mark

Replies are listed 'Best First'.
Re: Re: Re: question about || operator and list context
by BrowserUk (Patriarch) on Jun 22, 2003 at 04:29 UTC

    In this case, I would strongly favour using or instead of '||'. It removes the need for the extra parens and is (IMO) visually cleaner too.

    use File::Temp (qw/tempfile/); ($fh,$fname) = tempfile('quanta-XXXX') or die "Couldn't create temp file $fname\n";

    The (deliberately) much lower precedence of or means that it acts as an effective seperator between list contexts and in so doing, removes the need for a lot of otherwise necessary punctuation. TIMTOWTDI:)


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller