Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Putting a filename as a parameter to a perl script then reading from it in the code?

by johngg (Abbot)
on Dec 07, 2012 at 13:06 UTC ( #1007732=note: print w/replies, xml ) Need Help??


in reply to Putting a filename as a parameter to a perl script then reading from it in the code?

The nice thing about Perl is that it very often does what you want without you having to jump through hoops. If you supply a file name (or multiple file names) as an argument, Perl will automatically open the file for you so you can read it using an empty <> (readline) function. The following steps create a simple CSV file and an equally simple script to read it.

$ cat > xxx.csv Fred,male,25 Beth,female,31 Joe,male,22 $ cat > xxx #!/usr/bin/perl # use strict; use warnings; while ( <> ) # Read the file supplied as argument { chomp; # Remove line terminator my( $name, $sex, $age ) = split m{,}; printf qq{Name: %s\n Sex: %s\n Age: %s\n-----\n}, $name, $sex, $age; } $ chmod +x xxx $ ./xxx xxx.csv Name: Fred Sex: male Age: 25 ----- Name: Beth Sex: female Age: 31 ----- Name: Joe Sex: male Age: 22 ----- $

I hope this is helpful.

Cheers,

JohnGG

  • Comment on Re: Putting a filename as a parameter to a perl script then reading from it in the code?
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Putting a filename as a parameter to a perl script then reading from it in the code?
by Anonymous Monk on Dec 08, 2012 at 03:42 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2016-09-26 04:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:







    Results (475 votes). Check out past polls.