Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re^4: Dumb, non-question: How to return 'nothing'.

by ikegami (Pope)
on Jun 18, 2011 at 05:53 UTC ( #910299=note: print w/replies, xml ) Need Help??

in reply to Re^3: Dumb, non-question: How to return 'nothing'.
in thread Dumb, non-question: How to return 'nothing'.

Seeing as a scalar is always being returned,

while( my $next = $iter->() ) { ($next) = @$next; ... }

can be shortened to

while( my ($next) = @{ $iter->() } ) { ... }

Replies are listed 'Best First'.
Re^5: Dumb, non-question: How to return 'nothing'.
by Anonymous Monk on Jun 18, 2011 at 06:04 UTC

      No, the loop won't exit even if [ undef ] or [ 0 ] is returned. List assignment in scalar context returns the number of elements to which its RHS evaluates, meaning the number of elements in the array.

      You'd be right that

      while( my $next = $iter->() ) { my @next = @$next; ... }

      is not equivalent to

      while( my @next = @{ $iter->() } ) { ... }

      But that's not the situation here.

      The point was that

      while( my $next = $iter->() ) { ($next) = @$next; ... }

      is a complicated version of

      while( my $next = @{ $iter->() } ) { ... }

      which is just an expensive version of

      while( my ($next) = $iter->() ) { ... }
        The point was that while( my $next = $iter->() ) { ($next) = @$next; ... } is a complicated version of while( my $next = @{ $iter->() } ) { ... }

        Not so. In the latter case when the iterator returns undef to terminate iteration, you will attempt to dereference that undef causing an error.

        In the former, the loop terminates and the dereference is never reached.

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://910299]
[Discipulus]: new $workstation is arrived: SSD 250, sata 1TB, 16G RAM, only i5 CPU.. seems a good one: guess what first user installation will be?
[Corion]: Discipulus: Notepad++, or Perl ;)
[marto]: xubuntu or lubuntu
[Corion]: :-D
[marto]: my desktop is a core2 duo, 120GB SSD, 4GB RAM, boots to (GUI) login in less than 10 seconds from cold start
[Discipulus]: mah, winbuntu 10...
[marto]: whent he boys start school I'll build myself as new machine, ryzen based
[Corion]: marto: That's cool! I think my (home) desktop spends 30 seconds in the BIOS...
[Corion]: marto: Heh - currently they seem promising, but I think I'll stay with nVidia+Intel for the time being, as I've been bitten too often by bad AMD drivers
Discipulus is (still?) not a big fan of notepad++

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2017-07-27 08:52 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (407 votes). Check out past polls.