http://qs321.pair.com?node_id=964420


in reply to Re: Common Perl Pitfalls
in thread Common Perl Pitfalls

First solution is not equal to last two, as it implies that INPUTFILE is already open. I would say that the correct idiom looks like this:
my $slurp = do { open my $fh, '<', "inputfile"; local $/; <$fh> };

P.S. I really like the second one, thanks. Not for production use, of course. :)

Regards,
Alex.

Replies are listed 'Best First'.
Re^3: Common Perl Pitfalls
by JavaFan (Canon) on Apr 10, 2012 at 23:32 UTC
    Considering that Joe_'s example uses the handle INPUTFILE, I don't have any problems with the implication, and I really don't see the need to come up with the snobby term correct idiom. (You consider a piece of code with error handling to be correct idiom? You're fired).
    I really like the second one, thanks. Not for production use, of course. :)
    Why not? It's not significant different from your correct idiom. It misses error handling (but then, so does your correct idiom), but that's easily handled: just add a // die "slurp: $!";.

      Considering that Joe_'s example uses a handle and your last two use filename, it could be confusing for a novice.

      Why I wouldn't use the second example in production? Readability. Not everybody is familiar with intimate relationship between @ARGV and <>, and this solution doesn't really give any advantage over the common one.

      P.S. As for your show-off harshness, I care less for anti-snobs than I do for snobs. You didn't hire me, you have no authority to fire me. Have a good day sir.

      Regards,
      Alex.

        Not everybody
        Ah, but I don't have to write my production code for everybody. At best, only a hundred or so eyes will see my production code before it's obsolete or rewritten.