SPUG: "last" behaving oddly in -n loop

John W. Krahn krahnj at acm.org
Wed Aug 20 18:02:41 CDT 2003


On Wednesday 20 August 2003 13:55, Tim Maher wrote:
>
> I'm confused about how "last" is being handled with a -n
> (implicit) loop, which is very different from the way "next"
> is being handled.  I'd appreciate any insights!
>
> Here's the problem in a nutshell:
>
> $ cat last2
> print "$.: $ARGV" and last LINE ; END{print "File is $ARGV\n"}
>
> $ perl -wln last2 motd1 motd2
> 1: motd1
> File is motd1
> $
>
> Why didn't it process the second file, motd2, as happens with "next"?
> It's acting like last is magically allowed to break out of the outer
> (invisible) foreach loop, while next only affects the inner
> (invisible) while loop.

next and last are working as designed.  There is no (invisible) foreach 
loop.  <> processes the files in @ARGV as a continuous list of lines.  
If you want to move to the next file in @ARGV you have to explicitly 
close the ARGV filehandle.

print "$.: $ARGV" and close ARGV; END{print "File is $ARGV\n"}


John
-- 
use Perl;
program
fulfillment




More information about the spug-list mailing list