Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2015-05-30 20:57 GMT
Find Nodes?
    Voting Booth?

    In my home, the TV remote control is ...

    Results (600 votes), past polls