<?xml version="1.0" encoding="windows-1252"?>
<node id="1007732" title="Re: Putting a filename as a parameter to a perl script then reading from it in the code?" created="2012-12-07 08:06:06" updated="2012-12-07 08:06:06">
<type id="11">
note</type>
<author id="401112">
johngg</author>
<data>
<field name="doctext">
&lt;p&gt;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 [doc://open] the file for you so you can read it using an empty &lt;c&gt;&lt;&gt;&lt;/c&gt; ([doc://readline]) function. The following steps create a simple CSV file and an equally simple script to read it.&lt;/p&gt;
&lt;code&gt;
$ cat &gt; xxx.csv
Fred,male,25
Beth,female,31
Joe,male,22
$ cat &gt; xxx
#!/usr/bin/perl
#
use strict;
use warnings;

while ( &lt;&gt; ) # 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
-----
$
&lt;/code&gt;
&lt;p&gt;I hope this is helpful.&lt;/p&gt;
&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-401112"&gt;
&lt;p&gt;Cheers,&lt;/p&gt;&lt;p&gt;JohnGG&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1007712</field>
<field name="parent_node">
1007712</field>
</data>
</node>
