in reply to Beware of global! And bless the local!
G'day alexander_lunev,
A lesson well learned — thanks for posting.
"But why would anyone write about it again, and again, and again?"
I wouldn't be concerned with that. I've certainly urged people to use local in many posts; and not just for $/ but for other special variables which can have a global effect.
I actually take this a step further and use an anonymous block along the lines of
{ open my $fh, ... local $/; # do whatever with $fh here }
From the local documentation:
"A local modifies the listed variables to be local to the enclosing block, ..."
And from "perlvar: Variables related to filehandles":
"Usually when a variable is localized you want to make sure that this change affects the shortest scope possible."
Also note that the "= undef" is unnecessary; and the "close $fh" is implicit at the end of the anonymous block (see open for details). I'm not saying don't use them: just advising that they're optional (at least in the type of code you show in your example).
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Beware of global! And bless the local!
by alexander_lunev (Pilgrim) on Dec 12, 2019 at 13:26 UTC | |
by stevieb (Canon) on Dec 12, 2019 at 15:58 UTC | |
by Your Mother (Archbishop) on Dec 12, 2019 at 17:10 UTC | |
by stevieb (Canon) on Dec 12, 2019 at 17:37 UTC | |
by Fletch (Bishop) on Dec 12, 2019 at 17:59 UTC | |
by kcott (Archbishop) on Dec 13, 2019 at 05:01 UTC |