Perl6-Doc
view release on metacpan or search on metacpan
share/Apocalypse/A12.pod view on Meta::CPAN
as exportable, but more difficult to export something by default.
You no longer have to declare your tagsets separately, since C<:foo>
parameters are self-declaring, and the module will automatically
build the tagsets for you from the export trait arguments.
=head2 The gather/take Construct
We used one example of the conjectural gather/take construct. A gather
executes a closure, returning a list of all the values returned by
C<take> within its lexical scope. In a lazy context it might run as
a coroutine. There probably ought to be a dynamically scoped variant.
Unless it should be dynamic by default, in which case there probably
ought to be a lexically scoped variant...
=head2 :foo() Adverbs
There's a new pair syntax that is more conducive to use as option
arguments. This syntax is reminiscent of both the Unix command
line syntax and the I/O layers syntax of Perl 5. But unlike Unix
command-line options, we use colon to introduce the option rather than
the overly negative minus sign. And unlike Perl 5's layers options, you
share/Apocalypse/A12.pod view on Meta::CPAN
$string.pixels
Those last two require knowledge of the current font and rendering
engine, in fact. Though C<.columns> is likely to be pretty much the
same for most Unicode fonts that restrict themselves to single and
double-wide characters.
=head2 String positions
A corollary to the preceding is that string positions are not numbers.
If you say either
$pos = index($string, "foo");
or
$string ~~ /foo/; $pos = $string.pos;
then C<$pos> points to that location in that string. If you ask for
the numeric value of C<$pos>, you'll get a number, but which number you
share/Synopsis/S07-iterators.pod view on Meta::CPAN
other explicitly lazy operator:
1, (() ... { START mysub() }), 2 # harder way
1, lazy { mysub() }, 2 # easier way
[Note: the above text really belongs somewhere else, but I'm too lazy to
figure out where.]
=head1 Coroutines
Perl6 does not have a formally defined sub-style coroutine. Doubtless
there will be external modules to do so in different flavors. Such a
construct, where many calls made to the name of a sub with different
parameters, expecting to reach the same sub every time, is not really
compatible with multi-method-dispatch. While it may suit some
programming styles which only use a subset of the Perl6 language, it
cannot be made generally applicable across the Perl6 feature set.
This is not to say you cannot do coroutines in Perl6. In fact, the
gather/take construct is a simple coroutine. But if you want to pass
values back and forth Lua-style, you have to use a suplimentary
object:
sub my_coro (*@slurp) {
gather do {
my Int $initval;
my Str $secondval;
my Int $thirdval;
$initval = shift(@slurp);
$initval++;
take $initval;
$secondval = shift(@slurp);
take "$initval $secondval";
$thirdval = shift(@slurp);
take $thirdval + $initval;
}
}
my @a = (1);
my $it;
$it <== my_coro() <== while 1 { shift(@a) };
say "First result: " ~ get $it;
@a.push("Hello World");
say "Second result: " ~ get $it;
@a.push(500);
say "Third result: " ~ get $it;
...if you want to pass multiple parameters on each call, you can
use a slice slurpy instead, to pass a C<Capture>.
The iterator and array can of course be bundled up to give a more
natural feel:
class my_sub2coro {
has $!it = Failure;
has @!data = ();
has $.coro;
submethod BUILD {
$!it <== &($.coro)() <== while 1 { shift(@!data) };
}
method corocall($message) {
@!data.push($message);
get $!it;
}
}
my $c = my_sub2coro.new(coro => &my_coro);
say "First result" ~ $c.corocall(1);
say "Second result" ~ $c.corocall("Hello World");
say "Third result" ~ $c.corocall(500);
(XXX TODO: As feeds are not implemented yet, above example code not tested)
=head1 Additions
Please post errors and feedback to perl6-language. If you are making
a general laundry list, please separate messages by topic.
( run in 0.260 second using v1.01-cache-2.11-cpan-3cd7ad12f66 )