Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Cheap idioms

by broquaint (Abbot)
on Oct 14, 2002 at 08:43 UTC ( [id://205000]=note: print w/replies, xml ) Need Help??


in reply to Cheap idioms

If I'm ever in the position where I need to slurp a file I'll go with IO::File's handy getcontents() method
my $contents = IO::File->new($filename)->getcontents();
I'm not sure how that benchmarks, but I'm of the opinion that it looks much more elegant ;)
HTH

_________
broquaint

Replies are listed 'Best First'.
Re^2: Cheap idioms
by Aristotle (Chancellor) on Oct 14, 2002 at 11:04 UTC

    It looks very neat indeed, but it loads over 1,000 lines of Perl code and an XS module in dependencies.. in a short(!) CGI script I wouldn't want that - but that's always a difficult environment. It's too much to type in a oneliner also, but that too is a special case.

    For a large, "proper" script it is too brief - you run the risk of bombing out with a Can't call method "getcontents" on an undefined value since you don't check whether new() succeeded.. it would have to look maybe like this:

    my $contents = do { my $fh = IO::File->new($filename) or die "Failed to open $filename +: $!"; $fh->getcontents(); }
    And now it doesn't look so neat anymore. :-( Juerd's idiom on the other hand has builtin error reporting.

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://205000]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2024-04-18 12:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found