maddfisherman has asked for the wisdom of the Perl Monks concerning the following question:

how can you determine if a <> operator is on its last loop.
for example:
FILEHANDLE has "asdkjflaksdjf****askldfjas;lkdfj****sdkfj" $/ = "****"; while <FILEHANDLE> { if (last loop) {print NEWFILE "$_"} else {print OLDFILE "$_"} }

Replies are listed 'Best First'.
Re: end of
by japhy (Canon) on Aug 24, 2001 at 06:04 UTC
    while (<FH>) { if (eof FH) { # has read for the last time } }

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      I would just like to note (for maddfisherman's sake, not your's japhy as I'm sure you're already aware :) that eof will not work if you're reading data from a socket, pipe, or terminal. This is because, the way eof works is it tries to read a character from the file, noting an EOF if found, and then ungetc's the character back onto the file's stream so that you can read it in.

      Of course, this probably won't affect this situation but I wanted to point it out, JIC.

      bbfu
      Seasons don't fear The Reaper.
      Nor do the wind, the sun, and the rain.
      We can be like they are.

Re: end of
by Aighearach (Initiate) on Aug 24, 2001 at 05:52 UTC

    Instead, you might want to try something like

    local $/ = '****'; my @whole = <FILEHANDLE>; print NEWFILE pop @whole; #you might want to stick it in a temp scalar + first to make it clearer... some people think print() with side effe +cts is unclear print OLDFILE @whole;

    updated note: you don't have to use an array to do it this way, if you needed to actually process some of the input before all the input is received... you can do the same thing with a loop, but it's messy, because you'll be wanting to process the value you got on the _previous_ iteration, then process the last one outside the loop. You could also use sysread instead of <>, which will let you safely play with seeking and looking ahead for EOF, but unless you really really really need to do it that way, it's Bad News.
    --
    Snazzy tagline here