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.
![]() |
0 |
![]() |
Raymundo <gypark@gmail.com> wrote: > I am reading 'perlsub' documents: > http://perldoc.perl.org/perlsub.html > 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? Exactly right. > 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> ) ) { ... } It is even better practice to avoid package variables altogether! while (my $line = <STDIN>) { I teach this rule: Always prefer lexical variables (my) over package variables (local, our), except when you can't. > 2) If I am assigning to $_ in the loop for some reason, I have to > localize $_: > while ( <STDIN> ) { ... local $_ = something; ... } Yep, that is what you have to "look out" for (unless you use a lexical variable rather than a package variable). > Any help will be appreciated. See also: http://perl.plover.com/FAQs/Namespaces.html#What_Good_is_C_local_ Note also, that starting with perl 5.10, you can make $_ lexical: while (my $_ = <STDIN>) { See the "Lexical $_" heading in perldelta.pod from a recent perl version. -- Tad McClellan email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/" The above message is a Usenet post. I don't recall having given anyone permission to use it on a Web site.
![]() |
0 |
![]() |
On 4=BF=F924=C0=CF, =BF=C0=C0=FC1=BD=C303=BA=D0, Tad McClellan <ta...@seesi= g.invalid> wrote: > Raymundo <gyp...@gmail.com> wrote: > > Then, does the last sentence (with the sentence before it) mean: > > > 2) If I am assigning to $_ in the loop for some reason, I have to > > localize $_: > > while ( <STDIN> ) { ... local $_ =3D something; ... } > > Yep, that is what you have to "look out" for (unless you use > a lexical variable rather than a package variable). > Thank you very much!
![]() |
0 |
![]() |