> -----Message d'origine-----
> De�: Perl Mail User [mailto:[EMAIL PROTECTED]
> Envoy�: mardi 18 mai 2004 20:10
> ��: [EMAIL PROTECTED]
> Objet�: Perl Newbie Question
>
> Hello All,
>
> I have a question, I am looking to read the name of the file that I am
> passing as an argument to the perl script through the while (<>) part
of
> the script.
>
> Example: perl script.pl 1.txt 2.txt 3.txt
> Each file has information that I am parsing to put into a report but I
> need
> to get the name of the file that I parsed and print that information
as
> well,
> so that way I know what values I am getting from each file associates
with
> what file.
>
> So the output would look like
>
> 1.txt (parsed information)
> 2.txt (parsed information)
> 3.txt (parsed information)
>
> Any assistance would be great.
> Thanks
In addition to
perldoc perlvar
suggested in previous posts you may also give a look to
perldoc perlop
and search for "null filehandle".
It's said there :
<my_paste>
The null filehandle <> is special: it can be used to emulate the
behavior of sed and awk. Input from <> comes either from standard input,
or from each file listed on the command line. Here's how it works: the
first time <> is evaluated, the @ARGV array is checked, and if it is
empty, $ARGV[0] is set to ``-'', which when opened gives you standard
input. The @ARGV array is then processed as a list of filenames. The
loop
while (<>) {
... # code for each line
}
is equivalent to the following Perl-like pseudo code:
unshift(@ARGV, '-') unless @ARGV;
while ($ARGV = shift) {
open(ARGV, $ARGV);
while (<ARGV>) {
... # code for each line
}
}
except that it isn't so cumbersome to say, and will actually work. It
really does shift the @ARGV array and put the current filename into the
$ARGV variable.
</my_paste>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>