Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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

by Anonymous Monk
on Jun 17, 2011 at 15:53 UTC ( #910191=note: print w/replies, xml ) Need Help??

in reply to Dumb, non-question: How to return 'nothing'.

See Higher Order Perl, Chapter 4.5 THE SEMIPREDICATE PROBLEM
# array reference version sub make_iterator { ... return Iterator { my $return_value; ... if (exhausted) { return; } else { return [$return_value]; } } }

Replies are listed 'Best First'.
Re^2: Dumb, non-question: How to return 'nothing'.
by hardburn (Abbot) on Jun 17, 2011 at 20:09 UTC

    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.

      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?
      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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://910191]
[ELISHEVA]: Text::CSV_XS already can handle boms - see its detect_bom flag
[ELISHEVA]: but it seems that the DBD::CSV layer on top of it doesn't
[afoken]: Also: All other attributes that start with csv_ and are not described above will be passed to Text::CSV_XS (without the csv_ prefix).
[Discipulus]: what about a good 'ol SOPW?
[erix]: Giro d'Italia won by Dumoulin (peccato Nibali)
[ELISHEVA]: csv_detect_bom doesn't work either
[erix]: (SCNR)
[ELISHEVA]: I may have to resort to SOPW - but was hoping that this would be something simple
[erix]: I'd just remove the BOM, it is pretty simple
[ELISHEVA]: Simple yes. and I did consider that. but this isn't one off . An important data source that I don't control is generating bom prefixed utf8 files and I'd rather not have to be munging files every few months.

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2017-05-28 20:31 GMT
Find Nodes?
    Voting Booth?