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

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

by hardburn (Abbot)
on Jun 17, 2011 at 20:09 UTC ( #910253=note: print w/ replies, xml ) Need Help??


in reply to Re: 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? It'd make the iterating code just a bit cleaner:

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

As opposed to:

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

"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.


Comment on Re^2: Dumb, non-question: How to return 'nothing'.
Select or Download Code
Re^3: Dumb, non-question: How to return 'nothing'.
by BrowserUk (Pope) on Jun 17, 2011 at 20:45 UTC

    I think I've settled on requiring that the iterator be called in a list context. Eg.:

    sub i{ my $n = shift; return sub{ $n ? $n-- : () } };; $i = i( 5 ); print $_ while ( $_ ) = $i->();; 5 4 3 2 1

    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.
      Would be worthwhile to add carp "Iterator not called in list context" unless wantarray; so it can't be used improperly?

        Excellent notion. Thanks.


        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.
Re^3: Dumb, non-question: How to return 'nothing'.
by Anonymous Monk on Jun 18, 2011 at 05:16 UTC
    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 :)

      Seeing as a scalar is always being returned,

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

      can be shortened to

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2014-04-24 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (565 votes), past polls