Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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

by Corion (Pope)
on Aug 19, 2007 at 20:18 UTC ( #633658=note: print w/replies, xml ) Need Help??

in reply to what would you like to see in perl5.12?

The thing that is still missing in the Perl core for me is more flow control resp. non-linear flow:

  • Generators in Perl, so I can use (say):
    sub upto { my $stop = $_[0]; my $start = 0; for ($start..$stop) { yield $_ }; }; for my $item (upto(5)) { print "At $item\n"; }; __END__ At 1 At 2 At 3 At 4 At 5

    So far, Coro implements that, but not for Win32/MSVC, and it is not that stable. Also, the libcoro that Coro uses is under the GPL, which prohibits its distribution with the Perl core.

  • Lightweight threads that have the approach of "share everything" instead of the "share nothing" approach taken by Perl so far. There is Thread::Sociable, which implements that, but so far, the approach to Perl threads has been, different. All threading is problematic in Perl because of the semantics of the really global stuff, like the namespace - if you have a "share everything" approach, you need to protect %:: and everything hanging off there, or the following code will behave unpredictable:
    sub frobnicate { print "Frobnicating\n"; }; sub frobnitz { print "Frobnitzing\n"; }; sub gargle { local *frobnicate = \&frobnitz; frobnicate(); }; my $thr1 = async { gargle() }; my $thr2 = async { gargle() }; my $thr3 = async { gargle() };
  • Transparent asynchronous IO. When I use the following:
    open my $fh, "<:async", $filename or die "Couldn't open '$filename': $!"; my $line = <$fh>; # ... do some busy work without touching $line ... print $line;

    $line should be returned as a tie'd variable and the read request should be run in the background, allowing the foreground program to continue. Any access to $line will then wait until the background read operation has completed.

Update:Upon rereading, I don't need the :Generator syntax on my generators. Just using the yield keyword (similar to return but remember all state so we know where to continue when we get called again) is enough.

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

    What should:

    for my $item (upto(5)) { for my $other (upto(5)) { print "At $item and $other\n"; }; };
    mean? And how do I do the other? Can you give me an example where this will actually be usefull? And that can't be done better using closures.

    Share-everything threads were tried in perl 5.005.

    tie is slow. In either case you should already be able to implement this using PerlIO::via. If enough people use that, we might consider adding that to core.

      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.

        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?

Re^2: what would you like to see in perl5.12?
by blazar (Canon) on Aug 20, 2007 at 20:44 UTC
    The thing that is still missing in the Perl core for me is more flow control resp. non-linear flow:

    One thing that I miss both in Perl 5, 6 and most languages I know of is some form of addomesticated code rewriting. We're all told that goto is evil. But we have some forms of addomesticated goto: next, last and redo, which are not evil. We're all told that code rewriting is evil. But I'm sure that there could be useful and not evil forms of code rewriting. Limbic~Region speaks about that in Doing "it" only once.

      START blocks provide "do once" capability in Perl 6.
        START blocks provide "do once" capability in Perl 6.

        Whoa! I will stop complaining like whatever the common English idiom for a recurring, annoying and insisting whiner is.

        Would someone care how it would apply to the original (Perl 5) example?

        while ( <FH> ) { next if $_ eq 'foo'; # Where foo can only appear once in the file print; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://633658]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2017-03-22 23:16 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (277 votes). Check out past polls.