Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
P is for Practical
 
PerlMonks  

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

by Anonymous Monk
on Jun 18, 2011 at 05:16 UTC ( #910294=note: print w/ replies, xml ) Need Help??


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

That's a nice solution, but is there any particular reason to use an arrayref rather than a scalarref?

Its copy/paste from the book, the first solution -- I guess its more generic, re-usability and all that :)


Comment on Re^3: Dumb, non-question: How to return 'nothing'.
Re^4: Dumb, non-question: How to return 'nothing'.
by ikegami (Pope) on Jun 18, 2011 at 05:53 UTC

    Seeing as a scalar is always being returned,

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

    can be shortened to

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

        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->() ) { ... }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-04-18 05:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (461 votes), past polls