in reply to Command Switch -i prints to screen not file

You may be interested to know that you can achive the effect of the -i command line switch from inside a script via $^I, for an example see my node here. That way you have more control over what happens than the shebang line. (However, the limitation that ARGVOUT gets closed after the my @input = <>; still applies.)

Replies are listed 'Best First'.
Re^2: Command Switch -i prints to screen not file
by BillKSmith (Monsignor) on Dec 29, 2020 at 21:38 UTC
    Thanks to all for the explanation. I thought that this switch would add a useful, if not required, function with almost no code. Clearly this is not the case. If backup is desired, I would now recommend explicitly coding the steps described in the documentation of -i. Take care to put the close in the proper place.
    Bill
      Allow me some comments ...

      I think putting command-line switches into the she-bang is not one of the better ideas, I doubt it's portable.

      HaukeX just offered an alternative special variable, which even has a full English name $INPLACE_EDIT which is also self documenting.

      And you've rather got bitten here by the effects of slurping the whole file into an array which is often a dangerous idea, because the size might be huge. I prefer lazy iteration by working line by line, whenever possible.

      And making a full copy in RAM first is rather the strategy when you intend to directly replace the content in the filesystem, so you somehow doubled the efforts to a paradox effect.

      Just my 2 cents ... :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        I think putting command-line switches into the she-bang is not one of the better ideas, I doubt it's portable.

        I respectfully disagree. How else would you expect to enforce taint mode, for example?


        🦛