Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

RE: Return the contents of a file

by turnstep (Parson)
on Apr 12, 2000 at 18:02 UTC ( #7382=note: print w/replies, xml ) Need Help??


in reply to RE: Return the contents of a file
in thread Return the contents of a file

Here's another version:

sub readfile { local $/="" unless wantarray; return open(FS, shift) ? <FS> : undef; }
No 'my' variables, handles both string and array requests, and returns undef if the file could not be opened. Use it like this:

defined($slurp = &readfile($file)) or die "Could not open $file: $!\n" +; print "Scalar 'slurp' now has ", length $slurp, " characters in it\n"; defined(@slurp = &readfile($file)) or die "Could not open $file: $!\n" +; print "Array 'slurp' now has ", $#slurp, " elements in it.\n";

Replies are listed 'Best First'.
RE: RE: Return the contents of a file
by anders (Initiate) on May 26, 2000 at 17:41 UTC
    WOW!
    Just loves to see this.
    -anders

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2022-05-25 12:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (90 votes). Check out past polls.

    Notices?