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

Re: How to read from a file instead from a hard coding list.

by JavaFan (Canon)
on Apr 25, 2012 at 08:36 UTC ( [id://967020]=note: print w/replies, xml ) Need Help??


in reply to How to read from a file instead from a hard coding list.

Replace
my @words=qw( portion answers printer program );
with
chomp(my @words = `cat /path/to/file`);
and you should be done.

Replies are listed 'Best First'.
Re^2: How to read from a file instead from a hard coding list.
by afoken (Chancellor) on Apr 25, 2012 at 10:33 UTC
    my @words = `cat /path/to/file`

    Are you sure the OP uses some kind of unix? Windows has no cat command out of the box. And by the way: Why spawn a new process to read a file when perl can read the file without assistance? File::Slurp does a nice job.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Windows has no cat command out of the box.
      Does Windows has perl out of the box?
      Why spawn a new process to read a file when perl can read the file without assistance?
      Who cares about a new process? It isn't that we're doing this a billion times in a loop, and considering we're going to read a couple of Mb from disk, I don't think creating a new process is going to form a bottleneck.
      File::Slurp does a nice job.
      And that one comes with Windows out of a box? Really, you need a module to read in a file? What's wrong with
      chomp(my @words = do {local(@ARGV, $/) = "/path/to/file"; <>});
      or some other equivalent lines if you're willing to spend more keystrokes than `cat` is taking? Why is that using a module to do this is ok, but using a program isn't?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-03-29 10:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found