On Tue, 30 Sep 2008 11:26:09 +0800,
Amy Lee <> wrote:
> Hello,
>
> Here's my codes:
>
> foreach $file (@ARGV)
> {
> while (<>)
> {
If you want to read all files on the command line, one after the other,
simply use
while (<>) { }
There's no need to process @ARGV yourself for that.
See the perlop documentation. Look for the section starting with
The null filehandle <> is special: it can be used to emulate the behavâ€
ior of sed and awk
> Then I hope I can get a list of the result because I use "foreach" to
> process many files I gave at the same time. However, It just output one
> file and the output variable $CG is wrong. If I run this to process one
> file, it could work well.
I don't believe this. Even with the useless foreach loop around the
while loop, all files on the command line still should have been
processed.
This:
#!/usr/bin/perl
use strict;
use warnings;
foreach my $file (@ARGV)
{
while (<>)
{
print;
}
}
Works fine for me, and simply prints the contents of every file I give
it on the command line to STDOUT. It does this on the FIRST iteration of
the foreach loop. After this first iteration, @ARGV is empty, so there
is no second iteration. Since you're not supposed to modify arrays while
you're iterating over them, I suppose that this is current behaviour,
and not necessarily guaranteed. in other words: Don't do it. Either
iterate over @ARGV and handle the files yourself, or use while(<>), and
let it iterate.
Martien
--
|
Martien Verbruggen | Make it idiot proof and someone will make a
| better idiot.
|
|