Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I think that upto(...) returns an iterator/generator. The example I used is fairly trivial to reimplement in Perl5 now already:

# perl 5.10 sub ENDOFITERATION { undef }; # some magic value that signals the end +of all values sub upto { my $start = 0; my $stop = shift; return sub { if ($start < $stop) { return $start++ } else { return ENDOFITERATION }; }; };

Iterators/generators allow you to conveniently program in a linear fashion without needing to maintain the state. For example this contrived example is far easier to write in a linear fashion than it is in a closure fashion, because you need to store the current point of execution:

# Read incoming commands - an infinite loop/generator sub get_commands { while (<>) { yield $_; }; }; sub user_session { COMMAND: { my $command = get_commands; # read one command if ($command =~ /^login (\w+)/) { my $user = $1; my $pass = get_commands; # read next line if ($user_db{$user} ne $pass) { print "User $user rejected.\n"; redo COMMAND; } else { for my $command (get_commands) { if ($command =~ /^logout/) { return } else { yield [$user, $command] }; }; }; } else { # not logged in }; }; }; sub process_sessions { for my $line (user_session()) { my ($user,$command) = @$line; print "$user: Executing $command\n"; }; };

I'm aware that all my wishes are possible in principle now already, but either slow or burdened with ugly syntax. Which is why I wish for them to become less burdened and faster.


In reply to Re^3: what would you like to see in perl5.12? by Corion
in thread what would you like to see in perl5.12? by ysth

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

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

      Is guessing a good strategy for surviving in the IT business?





      Results (178 votes), past polls