[Edinburgh-pm] parallel semantics in perl

Marco Fontani fontani at gmail.com
Tue Dec 14 01:23:52 PST 2010


> However, the programmer might want to control this behaviour. So my
> question is, what would be an acceptable way of "overloading" Perl
> semantics to indicate sequential evaluation?

I think the acceptable way would be to _not_ overload them; see below.

> For example, whereas statements in a bare {...} block will be
> evaluated in parallel,  a do {...} could mean sequential evaluation. A
> "foreach" in full could be parallel, a "for" sequential.

foreach and for are the same thing really

$ perl -MO=Deparse -e 'foreach($i=0;$i<10;$i++){ print $i }'
for ($i = 0; $i < 10; ++$i) {
    print $i;
}
-e syntax OK

$ perl -MO=Deparse -e '@a=1..5;for(@a){print};foreach(@a){print}'
@a = 1..5;
foreach $_ (@a) {
    print $_;
}
foreach $_ (@a) {
    print $_;
}
-e syntax OK

> Any
> ideas/suggestion about this, e.g. how would you indicate that "map"
> should be sequential, etc?
> I obviously don't want to add new constructs to Perl 5.

I think you do.

At the LPW2010 Paul Evans presented his CPS module
(http://search.cpan.org/dist/CPS/lib/CPS.pm), which attempts to "do
stuff" in Continuation Passing Style. Basically you have new "kmap",
"kfoldl", "kfor" keywords which DWYW without changing the "usual" Perl
semantics.

You may want to go the same path: having a "pfor", a "pwhile" and
"pmap" rather than overloading the for, while etc. would mean that one
would know at-a-glance whether a certain block is executed in parallel
or not.

It would give you the best of both worlds:
- the programmer would be able to use the parallel version _or_ the
standard version, at will
- the programmer would still be able to keep specific stuff from
running in parallel (fors within fors?)

Since some *may* want instead to say, "all for/while/map in this
_scope_ are to be done in parallel", you may want to introduce
something similar to Aaron's suggested "use parallel" and "no
parallel" to import your parallel functions, locally, as the real
for/while/map, just only within the declared scope.

What are your thoughts?

-marco-

-- 
Marco Fontani

----
Glasgow Perl Mongers - http://glasgow.pm.org/
Join the RackSpace Cloud at: http://www.rackspacecloud.com/277.html


More information about the Edinburgh-pm mailing list