in reply to Re^3: Delete a line from files
in thread Delete a line from files

I meant why set $^I and $^/ at compile time with Begin?

Replies are listed 'Best First'.
Re^5: Delete a line from files
by soonix (Chancellor) on Oct 13, 2015 at 08:56 UTC
    These are documented in perlvar
    • for $^I search perlvar for "inplace". This has to be prepared before opening the file
    • $/ (without caret), if you use English, you can call it $INPUT_RECORD_SEPARATOR - should be set before the first read operation
    Update: the BEGIN block is necessary so that these assignments are executed before the implicit loop by the -p flag, see perlrun
      what is wrong with first opening the file with open and then setting those variables?
        It doesn't work. The diamond operator opens the file and reads from it at the same time, you can't first open the file and then set the variable. Check the difference yourself:
        perl -pwe '$^I = ".bak"; s/[<>]/|/g' file1 perl -pwe 'BEGIN { $^I = ".bak" } s/[<>]/|/g' file2
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ