×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

slurping files quickly

slurping files quickly

slurping files quickly

(OP)

Is there a one line way to slurp a file into an array?

Instead of
open(FILE, "file") ¦¦ warn "couldn't open file";
@array = <FILE>;
close(FILE);

I would like to
@array = <"file">;

I know, it's just a couple of lines, but Larry Wall himself says that lazyness is a virtue. :)

I saw someone do this:
@array = `/bin/cat file`;

but I want to stay in perl.

Thanks!
- Kai.

RE: slurping files quickly

For quick and dirty, it seems like 'cat' is a very reasonable way to do this.... unless you are interested in controlling the child process .... or .....you want to make the code portable to other OS's which might not have 'cat'....like a M$ windows PC.... ( which is available from Cygwin).

RE: slurping files quickly

Don't think you'd be able to do this, the reason being that you have to open the file before you can read it. No way around it (that I'm aware of). One way of simulating what you want would be to create a subroutine along the lines of:

sub slurp {
my $Filename = $_[0];
my @File_Array;
open(MYFILE, "<$Filename");
@File_Array = <MYFILE>;
close(MYFILE);
return(@File_Array);
}

Then call it as:

@File_Stuff = slurp("/path/to/my/file");

RE: slurping files quickly

(OP)
Yeah, that's kind of what I thought. Be handy if it were built in, but I guess I'll make a module similar to what you wrote. Thanks.

RE: slurping files quickly

The line...

@file = <ARGV>;

will actually slurp the contents of a filename passed in as a parameter without requiring that you open the file.  So, if you call the above line 'slurp.pl' and start the script as such...

perl slurp.pl test.txt

the entire contents of test.txt will be slurped into @file, even though you didn't explicity open it.

Strange, but true.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close