in reply to Re^2: Scope of $/
in thread Scope of $/

That is not true. No name is required. The {} set the scope of the local. For my demo below, I used an "our" variable, package scope. A Perl Global like $/ acts similar. You cannot localize a "my" variable.

I guess you can think of a local var as sort of like creating a stack of that variable. The local statement is like a "push" and when you exit the scope of the local, that is like a "pop". Rough analogy, but that is descriptive of the behavior.

#!usr/bin/perl use warnings; use strict; our $x = 1; #a package variable { print "$x\n"; #prints 1 local $x = 99; print "$x\n"; #prints 99 } print "$x\n"; #prints 1, back to before the "local"

Replies are listed 'Best First'.
Re^4: Scope of $/
by Anonymous Monk on May 26, 2016 at 01:52 UTC

    :) in the OPs program, having  { local $/ ... } on the outside doesn't change the behavior of the program,

    therefore no benefit is realized ... so the extra {local} is just extra typing (noise),

    unless you add a name, like  sub StdinTriElReader{ local $/ = "\n\n\n"; ... } then its serves a purpose

    :D:D:)

      Oh, I see that post didn't use local $/= "\n\n\n"; in a some kind of read statement, I presume that was an oversight. Oops. A common use of local is so that you don't have to remember what say, $/ "was". Just re-define it with local and when scope finishes, it "pops" back to what it was. That was my main point.
        Actually, the OP's code:
        while(<>){
        DOES use the $/="\n\n\n".

        My suggestion for the enclosing block is not strictly necessary for the posted code, but I think it is in good style, and enables protection in the event the program grows.

                This is not an optical illusion, it just looks like one.