in reply to Editing Files In Place

{ local $^I = ".bak"; local *ARGV; @ARGV = ".fetchmailrc"; while (<>) { s/^(here)/$username\n$1/i; print; } }

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: •Re: Editing Files In Place
by belg4mit (Prior) on Jun 13, 2002 at 00:13 UTC
    Oooh purdy. It'd always bugged me that one "had" to use -i. It's bit unfortunate you have to go the whole ARGV/STDIN route, but that's better than forking. thanks!

    --
    perl -pew "s/\b;([mnst])/'$1/g"

      the whole ARGV/STDIN
      Excuse me? There's no use of STDIN in that program. An empty diamond is the same as <ARGV>, not <STDIN>.

      And beginning Perl hackers who confuse the two will eventually get burned. My rule is to never use <> (aka <ARGV>) if you ever print some kind of prompt, and I flunk any use to the contrary in code review. Work it out for yourself to see why. The proper use for interaction where you prompt and read input is <STDIN>.

      -- Randal L. Schwartz, Perl hacker

        Well yes, I new it was the ARGV filehandle, but I tend to think of waka waka as STDIN. If @ARGV is empty waka waka reads from STDIN.

        (I'd even contemplated writing magic ARGV but decided against it ;-)

        --
        perl -pew "s/\b;([mnst])/'$1/g"

Re: •Re: Editing Files In Place
by bunnyman (Hermit) on Aug 25, 2003 at 21:49 UTC

    I just tried this on perl 5.005_02 and I found that it would not work with local *ARGV but local @ARGV worked.

    I assume localizing the ARGV filehandle somehow broke the <ARGV> magic. It works correctly on perl 5.6.0.