http://www.perlmonks.org?node_id=1007732


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

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