I can't understand a sentence "Look out for implicit assignments inwhile conditionals" in 'perlsub'

Discussion in 'Perl Misc' started by Raymundo, Apr 23, 2011.

  1. Raymundo

    Raymundo Guest

    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.
    Raymundo, Apr 23, 2011
    #1
    1. Advertising

  2. Raymundo

    Raymundo Guest

    Re: I can't understand a sentence "Look out for implicit assignmentsin while conditionals" in 'perlsub'

    On 4¿ù24ÀÏ, ¿ÀÀü1½Ã03ºÐ, Tad McClellan <> wrote:
    > Raymundo <> 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 $_ = 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!
    Raymundo, Apr 24, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. huhu

    Who can explain the sentence for me?

    huhu, Feb 29, 2004, in forum: C Programming
    Replies:
    8
    Views:
    312
  2. yezi

    Can I use that sentence ? Thanks

    yezi, Nov 4, 2005, in forum: C Programming
    Replies:
    10
    Views:
    517
    Simon Biber
    Nov 17, 2005
  3. sword
    Replies:
    2
    Views:
    322
    Sumit RAJAN
    Aug 3, 2006
  4. stonny
    Replies:
    6
    Views:
    310
    Jerry Coffin
    Aug 4, 2006
  5. SomeDeveloper

    Question on a 'perlsub' statement.

    SomeDeveloper, Jul 7, 2009, in forum: Perl Misc
    Replies:
    17
    Views:
    120
Loading...

Share This Page