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
In reply to Re: Beware of global! And bless the local!
by kcott
in thread Beware of global! And bless the local!
by alexander_lunev
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |