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

Re^3: what would you like to see in perl5.12?

by Corion (Patriarch)
on Aug 20, 2007 at 15:36 UTC ( [id://633851]=note: print w/replies, xml ) Need Help??


in reply to Re^2: what would you like to see in perl5.12?
in thread what would you like to see in perl5.12?

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.

Replies are listed 'Best First'.
Re^4: what would you like to see in perl5.12?
by Jenda (Abbot) on Aug 20, 2007 at 23:00 UTC

    If upto() indeed returned an iterator that you could then call, all would be well, but that doesn't seem to have happened. If it did then your original code would look somewhat like this:

    my $iter = upto(5); while (my $item = $iter->()) { print "Item: $item\n"; }
    With this the intent would be clear. And you would be able to specify exactly what do you want. I'm not talking about the implementation of upto(), I'm talking about its use! In the example I gave you, with the two nested loops, are the two upto(5) things the same iterator or two separate ones? If they are separate then how come you apparently assume the get_commands() to be a single iterator? And if it's the same iterator, how do I create a separate one?

    Imagine you wanted to make the get_commands() more general and allow you to read the commands from a file specified by the filename. Now am I supposed to pass the same filename to all occurences of the get_commands()? Or just the first and all following will use that file? How will I know they are not trying to access the <>? And how do I open two iterators at once?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-03-28 11:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found