Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: When does $_ get used?

by mexnix (Pilgrim)
on Jul 26, 2001 at 22:08 UTC ( [id://100073]=note: print w/replies, xml ) Need Help??


in reply to When does $_ get used?

I didn't really get your question at first, but after reading the replies, esp. dragonchild, I understood what was going on. If you use while (my $elm = shift @list) { some_funct $elm } (or while ($_ = shift @list) {)that will work. I doubt, have no clue, that this is coding practice, but it works. All the other replies are good, this is just using while.

__________________________________________________
<moviequote name="The Whole Nine Yards">
Jimmy T: Oz, we're friends, friends do not engage in sexual congress with each others wives.
</moviequote>

mexnix.perlmonk.org

Replies are listed 'Best First'.
(MeowChow) Re2: When does $_ get used?
by MeowChow (Vicar) on Jul 26, 2001 at 22:37 UTC
    It is indeed bad coding practice, unless the following behaviour is actually desired:
      
    my @l = (1, 2, 3, 0, 5, 6); while (my $x = shift @l) { print "$x "; } ## OUTPUT ## 1 2 3
    The correct idiom is:
      
    my @l = (1, 2, 3, 0, 5, 6); while (@l) { my $x = shift @l; print "$x "; }
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
      My idea, and what I thought elbie's idea was, is treating the @list like a filehandle (i.e. the ever used while (my $line = <STDIN>) { something_with $line }). However, I submit to your [much] greater knowlegde, and vow never to do it myself.

      __________________________________________________
      <moviequote name="The Whole Nine Yards">
      Jimmy T: Oz, we're friends, friends do not engage in sexual congress with each others wives.
      </moviequote>

      mexnix.perlmonk.org

        Actually, Perl treats filehandle input within while loops as a special case, to avoid exactly this problem, and to allow for a more natural idiom. To quote the relevant bit of arcana from the POD:
          
        while (my $line = <STDIN>) { print $line }
        In these loop constructs, the assigned value (whether assignment is automatic or explicit) is then tested to see whether it is defined. The defined test avoids problems where line has a string value that would be treated as false by Perl, for example a ``'' or a ``0'' with no trailing newline. If you really mean for such values to terminate the loop, they should be tested for explicitly...

           MeowChow                                   
                       s aamecha.s a..a\u$&owag.print
        while is, in my opinion, generally waaaay over-used. foreach generally does everything you want in a while, and more (like assigning to $_). If you want to treat @list like a filehandle, then do a foreach(@list).

        As a sidenote, you can also do something like:

        foreach (<>) { # Do something with $line here... }
        What that does is require that all the input be done, then release the output. This is unlike while (<>), which will pass its value through to the loop immediately. The difference is because foreach needs to know when it ends, while while doesn't care and will keep going until it's given a false value.
        Reading from a file doesn't have the "false false" problem on reading a 0 or empty line because the value read includes the trailing "\n" so will never be empty. As for why "0\n" isn't treated as false, I don't know -- normally the trailing stuff in a string is silently ignored, but (experimenting...) it seems that a string that converts to a value of zero is still true if it has stuff after it other than spaces (and maybe other things -- not tested everything, but not \n so not whitespace in general). But it's still zero in an expression, so "0 " is false, "0 pigs" is true, and 0+"0 pigs" is false. It's one of those places where Perl does what you want, most of the time, but is hard to understand if you want complete details.

        —John

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://100073]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-03-29 08:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found