Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: list context (iterators)

by LanX (Bishop)
on Dec 07, 2010 at 00:12 UTC ( #875713=note: print w/replies, xml ) Need Help??


in reply to list context

in my $x = 10; the = is a scalar assignment which returns the assigned scalar

in my ($y) = 10; the = is a list assignment which returns the number of assigned elements╣

That's a crucial trick for iterators, where returned elements might be false (like 0 , '' or undef).

So

while ( ($x) = iterator() ) { ... }
will work as long as anything (i.e. no empty list) is returned, while
while ( $x = iterator() ) { ... }
can break if you don't use special magics like "0 but true".

Cheers Rolf

UPDATE:

1) in scalar context!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://875713]
help
Chatterbox?
NodeReaper stokes the furnace

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (1)
As of 2018-07-22 01:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (451 votes). Check out past polls.

    Notices?