in reply to Avoid re-invocation for -pie processing

local @ARGV = $path; local $^I = ""; while ( <> ) { s/foo/bar/g; print; }

Assumes ARGV is currently at EOF.

Replies are listed 'Best First'.
Re^2: Avoid re-invocation for -pie processing
by Corion (Patriarch) on Jan 08, 2024 at 15:29 UTC

    I sometimes use the following, which also resets *ARGV and $ARGV:

    local *ARGV = [$path];

    ... but most of the time, I forget about the other slots...

    Update: And I learned something. That stanza above does not reset $ARGV:

    perl -wlE 'sub say_argv { say @ARGV, $ARGV; }; $ARGV=9; say_argv; do { + local *ARGV = ["foo"]; say_argv; }; say_argv' 1 2 3 1239 foo9 1239

      If you're not in the middle of using <> (since the snippet I posted doesn't work if you're already in the middle of reading from ARGV), then it doesn't really matter if you clobber *ARGV{IO} and $ARGV.