Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hey, im using inplace file editing in my script (not setting the -i flag, but setting @ARGV and then $^I from within the code) and doing while(<>) { stuff } doesnt work right if you set $/ to an empty string ($/="")...usually setting $/ to "" and then going through a while loop over a file will cause everything to be read in one go, but using inplace editing, it seems to read through multiple times. is there any way to get around this?

Replies are listed 'Best First'.
Re: input record separator and inplace editing
by bikeNomad (Priest) on Jun 06, 2001 at 22:07 UTC
    Setting $/ to "" will treat empty lines as a record terminator. This is different from setting it to undef, which will allow you to slurp the whole file in one <>.
Re: input record separator and inplace editing
by tachyon (Chancellor) on Jun 07, 2001 at 00:06 UTC

    Yes, undef $/ rather than setting it to null as this will give you the desired result. Using a block and local will save having to remember to reset it which can avoid nasty suprises.

    { local $/; # this undefines $/ within this block only $all = <FH>; } # $/ is not undefined here so input works as normal again

    tachyon