http://www.perlmonks.org?node_id=910294


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