in reply to File editing

OK, I'll give it a try:

Replies are listed 'Best First'.
RE: Re: File editing
by Fastolfe (Vicar) on Sep 29, 2000 at 00:01 UTC
    You need to set $_ explicitely here.
    perl -i -p -e '$_ = <> until $ok || m/RE/; $ok=1' foo

      Darn! I never realized that the <> did not assign $_. Why o why?

      In any case thanks Fastolfe and dchetlin for pointing this out.

        Well it can assign $_. My mistake above and your originally is that <> only assigns $_ if it is the only thing inside the conditional of a while loop. I guess after using it that way for years I forgot it was the only way!

        I should have consulted the Camel before I posted my errors. The funny thing is what I posted does work sometimes (specifically it works with the two tests I used - there's a lesson there too), but also fails in unexpected (by me) ways.

RE: Re: File editing
by dchetlin (Friar) on Sep 28, 2000 at 23:57 UTC

    Wow ... peek into my brain as I look at your code above:

    • Why would that work? What on earth are you trying to do?
    • Oh, I see ... hmm, why doesn't that work? It's bizarre, but it should...
    • Ah, duh. The magic diamond operator doesn't automatically assign to $_ -- it's the while magic that does that. So you're comparing the REx against the same $_ the whole time, causing an infinite loop.

    Even if you did assign your diamond operator to a variable, you're still going to accidentally let the first line of the file through, because it's already been pulled out by the implicit while (<>) { ... } continue { print } that the -p switch gives you. Basically, using the diamond operator inside of a -p or -n script is going to do strange things. I'd avoid it. See my note further down on a non-confusing way to do this :-)

    -dlc

RE: Re: File editing
by Albannach (Monsignor) on Sep 28, 2000 at 21:32 UTC
    I tried it and got something, but this spits out from the LAST line that contains a match to the end of the input file, and I can't see why it does that. How can I change this to spit out from the FIRST occurance of the RE?

    perl -i -n -e "unless($ok || /RE/) {<>} else{print;$ok=1}" foo