Perl6-Doc

 view release on metacpan or  search on metacpan

share/Apocalypse/A03.pod  view on Meta::CPAN

should start getting used to the idea that C<mumble($foo)> is
equivalent to C<$foo.mumble()>, at least in the absence of a local
subroutine definition to the contrary. So I expect that we'll see both:

    is_readable($file)

and:

    $file.is_readable

Similar to the cascaded filetest ops in the previous section, one
approach might be that the boolean methods return the object in
question for success so that method calls could be stacked without
repeating the object:

    if ($file.is_dir
             .is_readable
             .is_writable
             .is_executable) {

[Update: the syntax above is now illegal.]

share/Apocalypse/A03.pod  view on Meta::CPAN


There is also the question of whether this really saves us anything
other than a little notational convenience. If each of those methods
has to do a I<stat> on the filename, it will be rather slow. To fix
that, what we'd actually have to return would be not the filename, but
some object containing the stat buffer (represented in Perl 5 by the
C<_> character). If we did that, we wouldn't have to play C<$file is
true> games, because a valid stat buffer object would (presumably)
always be true (at least until it's false).

The same argument would apply to cascaded filetest operators we talked
about earlier. An autoloaded C<-drwx> handler would presumably be smart
enough to do a single stat. But we'd likely lose the speed gain by
invoking the autoload mechanism. So cascaded operators (either C<-X>
style or C<.is_XXX> style) are the way to go. They just return objects
that know how to be either boolean or stat buffer objects in context.
This implies you could even say

    $statbuf = -f $file or die "Not a regular file: $file";
    if (-r -w $statbuf) { ... }

This allows us to simplify the special case in Perl 5 represented by
the C<_> token, which was always rather difficult to explain. And
returning a stat buffer instead of C<$file> prevents the confusing:

share/Apocalypse/A04.pod  view on Meta::CPAN

    $result = given $digit {
        when "T" { 10 }
        when "E" { 11 }
        default  { $digit }
    }

Unlike in C, the C<default> case must come last, since Perl's cases are
evaluated (or at least pretend to be evaluated) in order. The optimizer
can often determine which cases can be jumped to directly, but in cases
where that can't be determined, the cases are evaluated in order much
like cascaded C<if>/C<elsif>/C<else> conditions. Also, it's allowed to
intersperse ordinary code between the cases, in which case the code
must be executed only if the cases above it fail to match. For example,
this should work as indicated by the print statements:

    given $given {
        print "about to check $first";
        when $first { ... }
        print "didn't match $first; let's try $next";
        when $next { ... }
        print "giving up";

share/Apocalypse/A06.pod  view on Meta::CPAN

the list part, it really needs to earn its keep, and do more work.
A special operator could also force scalar context on the left and
list context on the right.  So with implied scalar context we could
omit the backslash above:

    @args = (@foo with 1,2,3);
    stuff *@args;

That's all well and good, and some language designers would stop
right there, if not sooner.  But if we think about this in relation
to cascaded list operators, we'll see a different pattern emerging.
Here's a left-to-right variant on the Schwartzian Transform:

    my @x := map {...} @input;
    my @y := sort {...} with @x;
    my @z := map {...} with @y;

When we think of data flowing left-to-right, it's more like a pipe
operator from a shell, except that we're naming our pipes C<@x>
and C<@y>.  But it'd be nice not to have to name the temporary
array values.  If we do have a pipe operator in Perl, it's not going

share/Synopsis/S02-bits.pod  view on Meta::CPAN

In order to interpolate the result of a method call without arguments,
it's necessary to include parentheses or extend the call with something
ending in brackets:

    print "The attribute is $obj.attr().\n"
    print "The attribute is $obj.attr<Jan>.\n"

The method is called in item context.  (If it returns a list,
that list is interpolated as if it were an array.)

It is allowed to have a cascade of argumentless methods as long as
the last one ends with parens:

    print "The attribute is %obj.keys.sort.reverse().\n"

(The cascade is basically counted as a single method call for the
end-bracket rule.)

=item *

Multiple dereferencers may be stacked as long as each one ends in
some kind of bracket or is a bare method:

    print "The attribute is @baz[3](1, 2, 3).gethash.{$xyz}<blurfl>.attr().\n"

Note that the final period above is not taken as part of the expression since

share/Synopsis/S09-data.pod  view on Meta::CPAN

virtual PDL where the new array aliases its values back into the
old one.)

Of course, a single element can be selected merely by providing a single
index value to each slice list:

    @x[0;1;42]

=head1 Cascaded subscripting of multidimensional arrays

For all multidimensional array types, it is expected that cascaded subscripts:

    @x[0][1][42]
    @x[0..10][1,0][1,*+2...*]

will either fail or produce the same results as the equivalent
semicolon subscripts:

    @x[0;1;42]
    @x[0..10; 1,0; 1,*+2...*]

Built-in array types are expected to succeed either way, even if
the cascaded subscript form must be implemented inefficiently by
constructing temporary slice objects for later subscripts to use.
(User-defined types may choose not to support the cascaded form, but
if so, they should fail rather than providing different semantics.)
As a consequence, for built-in types of declared shape, the appropriate
number of cascaded subscripts may always be optimized into the
semicolon form.

=head1 The semicolon operator

At the statement level, a semicolon terminates the current expression.
Within any kind of bracketing construct, semicolon notionally separates
the sublists of a multidimensional slice, the interpretation of
which depends on the context.  Such a semicolon list puts each of its
sublists into a C<Parcel>, deferring the context of the sublist until
it is bound somewhere.  The storage of these sublists is hidden in

share/Synopsis/S12-objects.pod  view on Meta::CPAN


    @list.grep: { $_ % 2 }.map: { $_ - 1 }.say

and that will be taken as equivalent to

    @list.grep({ $_ % 2 }).map({ $_ - 1 }).say

Since the colon does not require a space in this case, and it looks
slightly odd there anyway, it may be clearer to omit the space to make
the method calls on the right look more like they attach to the term
on the left in one cascade of method calls:

    @list.grep:{ $_ % 2 }.map:{ $_ - 1 }.say

Methods (and subs) may be declared as lvalues with C<is rw>.  You can
use an argumentless C<rw> method anywhere you can use a variable,
including in C<temp> and C<let> statements.  (In fact, you can use an
C<rw> method with arguments as a variable as long as the arguments are
used only to identify the actual value to change, and don't otherwise
have strange side effects that differ between rvalue and lvalue usage.
Setter methods that expect the new value as an argument do not fall



( run in 0.635 second using v1.01-cache-2.11-cpan-49f99fa48dc )