R
Raymundo
Hello Perl users,
First of all, I'd have to tell you that I'm not so good at English,
not at Perl either.
I am reading 'perlsub' documents:
http://perldoc.perl.org/perlsub.html
In the section "When to still use local()", it says:
---
Despite the existence of my, there are still three places where the
local operator
still shines. In fact, in these three places, you must use local
instead of my.
1.
You need to give a global variable a temporary value, especially $_.
The global variables, like @ARGV or the punctuation variables, must
be localized with local(). This block reads in /etc/motd, and splits
it up
into chunks separated by lines of equal signs, which are placed in
@Fields .
{
local @ARGV = ("/etc/motd");
local $/ = undef;
local $_ = <>;
@Fields = split /^\s*=+\s*$/;
}
It particular, it's important to localize $_ in any routine that
assigns to it.
Look out for implicit assignments in while conditionals.
2.
....(omitted)...
---
("It particular" seems to be a typing error of "In particular")
Okay, I can (maybe) understand the point of this paragraph:
- I can (and I have to) use local() to localize the global variables
Then, what is the exact meaning of the last sentence?
"Look out for implicit assignments in while conditionals."
I guess "implicit assignments in while contitionals" are referrign to
the code like
while ( <STDIN> ) { ... }
because it is, in fact,
while ( defined ( $_ = <STDIN> ) ) { ... }
right?
First, I found that $_ is NOT localized automatically in the
conditional by assigning some value to $_ before the loop and printing
it after the loop.
Then, does the last sentence (with the sentence before it) mean:
1) It is better practice to localize $_ explicitly in the conditional:
while ( defined ( local $_ = <STDIN> ) ) { ... }
or
2) If I am assigning to $_ in the loop for some reason, I have to
localize $_:
while ( <STDIN> ) { ... local $_ = something; ... }
or
3) just "be aware and cautious, $_ isn't be localized automatically"
or
4) I am totally missing the point now :-/ It means something else
??
Any help will be appreciated.
First of all, I'd have to tell you that I'm not so good at English,
not at Perl either.
I am reading 'perlsub' documents:
http://perldoc.perl.org/perlsub.html
In the section "When to still use local()", it says:
---
Despite the existence of my, there are still three places where the
local operator
still shines. In fact, in these three places, you must use local
instead of my.
1.
You need to give a global variable a temporary value, especially $_.
The global variables, like @ARGV or the punctuation variables, must
be localized with local(). This block reads in /etc/motd, and splits
it up
into chunks separated by lines of equal signs, which are placed in
@Fields .
{
local @ARGV = ("/etc/motd");
local $/ = undef;
local $_ = <>;
@Fields = split /^\s*=+\s*$/;
}
It particular, it's important to localize $_ in any routine that
assigns to it.
Look out for implicit assignments in while conditionals.
2.
....(omitted)...
---
("It particular" seems to be a typing error of "In particular")
Okay, I can (maybe) understand the point of this paragraph:
- I can (and I have to) use local() to localize the global variables
Then, what is the exact meaning of the last sentence?
"Look out for implicit assignments in while conditionals."
I guess "implicit assignments in while contitionals" are referrign to
the code like
while ( <STDIN> ) { ... }
because it is, in fact,
while ( defined ( $_ = <STDIN> ) ) { ... }
right?
First, I found that $_ is NOT localized automatically in the
conditional by assigning some value to $_ before the loop and printing
it after the loop.
Then, does the last sentence (with the sentence before it) mean:
1) It is better practice to localize $_ explicitly in the conditional:
while ( defined ( local $_ = <STDIN> ) ) { ... }
or
2) If I am assigning to $_ in the loop for some reason, I have to
localize $_:
while ( <STDIN> ) { ... local $_ = something; ... }
or
3) just "be aware and cautious, $_ isn't be localized automatically"
or
4) I am totally missing the point now :-/ It means something else
??
Any help will be appreciated.