Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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'.

Replies are listed 'Best First'.
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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2018-06-24 09:56 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.