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.