Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re^2: perl -i -pe ... with eof() testing

by Anonyrnous Monk (Hermit)
on Feb 08, 2011 at 19:57 UTC ( #887040=note: print w/replies, xml ) Need Help??

in reply to Re: perl -i -pe ... with eof() testing
in thread perl -i -pe ... with eof() testing

This is what I figured, too (kind of). But why does it then "work" with the eof form?  This produces

--- cat before --- line1 line2 line3 line1 line2 line3 --- script output to stdout --- --- cat after --- line1 line2 line3 opt=bar line1 line2 line3 opt=bar

(I say "work" in quotes because it's not what is needed here, but what I would've expected.)

Also, perl -MO=Deparse ... doesn't highlight any difference, except the eof vs. eof() itself.

Replies are listed 'Best First'.
Re^3: perl -i -pe ... with eof() testing
by ikegami (Pope) on Feb 08, 2011 at 20:05 UTC

    The only way (eof) can check for per-file eof as documented would be if it read from the real file handle instead of the magical handle. The magical handle doesn't move to the next file because it's not used.

      Thanks, you're right.

      Just for the record: I had confused myself unnecessarily by not putting a separator in between the cat of file1 and file2. Which made me wonder why the magical file handle is only being advanced by eof() at the very end, and not at every file boundary.  When you do it properly, however, i.e. something like

      cat file1 echo --- cat file2

      the output is

      ... --- perl output to stdout --- line3 opt=bar --- cat after --- line1 line2 --- line3 line1 line2

      which shows that line3 of the first file does in fact belong to file2 after the processing...

      P.S. I've now settled on suggesting my collegue

      perl -i -pe '$f||=s/opt=.*/opt=bar/; $_.="opt=bar\n" if !$f && !@ARGV +&& eof' files...

      (but unfortunately it took a little too long to get working to leave an "it's easy with Perl" impression — that's my fault, however :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://887040]
[Discipulus]: test for pmcbtk
[Discipulus]: sometimes the client dies for extra content </chatter><span class="error" style="color: #cc0000">Server Error (Error ID 9162
[Discipulus]: the span is considered extra content by the parser
[Corion]: Mhmm - that would be some internal error, but I can't look into that right now as I don't have access to the logs from here ;)
[Discipulus]: ah, but it can be skipped by the client (?)
[Corion]: I think so, but I wonder what error might be there...
[Discipulus]: raining in germany? ;=)

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (13)
As of 2017-07-25 08:11 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (366 votes). Check out past polls.