SPUG: flip-flops

Yitzchak Scott-Thoennes sthoenna at efn.org
Wed Feb 13 16:10:06 CST 2002


In article <wkr8nqmck3.fsf at att.net>,
"Michael R. Wolf" <MichaelRunningWolf at att.net> wrote:
>perl -n -e '$body   .= $_ if /^$/ .. eof();' \
>        -e '$head   .= $_ if (1 .. /^$/);' \
>        -e 'END{print $head, "\n", "="x40, "\n", $body;}'

How about:

perl -pe'(/^$/..eof&&(close*ARGV||1))==1and$_.=("="x40)."\n"'

>BTW -- Matt's code would work for 1 file as input, but since
>the body flip-flop latches on, it will miss the EOF
>condition as subsequent files are processed.  If this code
>was meant to process multiple files (via <>), the eof() test
>is better, but would necessitate a "close ARGV if eof()" to
>allow resetting $..  Other problems with this snippet that
>prevent it from looping over multiple files, so it's a good
>example.

You don't have it quite right.  perlfunc says:
             In a "while (<>)" loop, "eof" or "eof(ARGV)" can
             be used to detect the end of each file, "eof()"
             will only detect the end of the last file.  Exam-
             ples:

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org





More information about the spug-list mailing list