SPUG: "last" behaving oddly in -n loop

Yitzchak Scott-Thoennes sthoenna at efn.org
Thu Aug 21 11:17:03 CDT 2003


I think you are under a misconception.  <> aka <ARGV> aka readline(ARGV)
doesn't wrap an extra loop around your code (whether you use -n or not)
so your last LINE falls of the end of your program.

For instance, in the code:

#!/usr/bin/perl -wpl
# merge with next line if line ends with \
chop,chomp($_.=<>) while substr($_,-1) eq '\\';

both the implicit <> because of -p and the explicit <> will do the
magic-ARGV thing of looping through the files in @ARGV.  In the
explicit <>, there is clearly no way for perl to magically insert a
loop around part of your code.  Instead, the magic of using each file
in @ARGV in turn occurs in the guts of the readline builtin.  (Fixing
this so that anything--not just readline--using the ARGV handle loops
through all the files is on the list in perltodo.pod.)

To do what you want, change last2 from:
  print "$.: $ARGV" and last LINE; END{print "File is $ARGV\n"}
to:
  print "$.: $ARGV" and close ARGV; END{print "File is $ARGV\n"}

(Obviously you would want to also have "next LINE" after the close
if there were other code following.)



More information about the spug-list mailing list