http://qs1969.pair.com?node_id=205088


in reply to Re: Cheap idioms
in thread Cheap idioms

select( ( select(STDOUT), $| = 1 )[0] );

This only works if the inner select is evaluated before the assignment is, and I can't find any specification of evaluation order (remember ++$a, $a++, ++$a?)

That's why I don't dare to use this idiom, although I see it often. I still prefer STDOUT->autoflush(1) (using IO::Handle). It's shorter too :)

- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.

Replies are listed 'Best First'.
Re^3: Cheap idioms
by Aristotle (Chancellor) on Oct 14, 2002 at 14:35 UTC
    Ah, but you're wrong. In a simple list, as perlop explicitly states, operands are evaluated left-to-right. Otherwise, you couldn't write print("Done.\n"), exit if $done;

    either, but we all know that it works, right? Your example is problematic because is uses pre- and postincrement operators which mess with the order of side effects.

    IO::Handle loads over 1,000 lines of code - if it's just for a single autoflush, what's the point? Especially seeing as the select idiom is known and even delivered with the perldocs, it's safe to assume it isn't cryptic.

    Makeshifts last the longest.

      Im sorry but I fail to understand how both these idioms work. I can see why it slurps a file, but cant really say exactly how... Could somebody explain the magic with ARGV?

        The diamond operator (<>) will go through all elements of @ARGV in sequence, open and read them, one after another. So you localize @ARGV, meaning you make a "private copy" of it for the duration of the block, put just one filename in it, the one you wish to read, and also make a local copy of $/ (which contains what Perl is to assume an end-of-line; see perldoc perlvar) without actually putting any value in it, meaning you'll slurp the entire content from any file handle at once. As the diamond operator is the last thing in the block, its result becomes the "return value" of the block.

        And thus, the slurped file content ends up in the variable.

        Makeshifts last the longest.