Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 17:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found