Tom Boy
Tom Boy

Reputation: 659

Need help understanding this perl code

$pattern = shift(@ARGV)
while(<ARGV>){
    if(/$pattern/){
        print $ARGV,": ",$_;
    }
}

The command: ./myprog.pl Size File*

Where File1:

SetSize
ResetSize
SETSIZE
resetSIZE

File2:

This is a new file
and its Size is very small.

The output:

File1: SetSize
File1: ResetSize
File2: and its Size is very small

Could someone please explain that main code to me. I really dont understand how there are two File print outs where each while loop is running each argument once and there is only 1 if condition. How is arg1 (File 1) outputting twice?

Upvotes: 1

Views: 56

Answers (1)

Amadan
Amadan

Reputation: 198324

while(<ARGV>) is a bit of Perl magic: it will give you lines from each file listed in @ARGV. Relevant docs: perlio: I/O Operators and perlvar: ARGV.

Upvotes: 3

Related Questions