Changeset 21934 for docs/Perl6/Perl5

Show
Ignore:
Timestamp:
08/18/08 16:52:14 (4 months ago)
Author:
moritz
Message:

[docs] fixes and enhancments to Perl6::Perl5::Differences

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • docs/Perl6/Perl5/Differences.pod

    r17461 r21934  
    7575    Was:    $array[$#array] 
    7676    Now:    @array[@array.end] 
    77             @array[-1]              # also works 
     77            @array[*-1]              # beware of the "whatever"-star 
    7878 
    7979For details, see L<S02/"Built-In Data Types"> 
     
    9292twigil refers to data that is known at compile time. 
    9393 
     94=head2 Context 
     95 
     96There are still three main contexts, void, item (formerly scalar) and list. 
     97Aditionally there are more specialized contexts, and operators that force that 
     98context. 
     99     
     100    my @array = 1, 2, 3; 
     101 
     102    # generic item context 
     103    my $a = @array; say $a.WHAT;    # prints Array 
     104 
     105    # string context 
     106    say ~@array;                    # "1 2 3" 
     107 
     108    # numeric context 
     109    say +@array;                    # 3 
     110     
     111    # boolean context 
     112    my $is_nonempty = ?@array; 
     113 
    94114=cut  
    95115 
     
    103123Some highlights: 
    104124 
    105 =head2 C<qw()> has a customary form; new interpolating form 
     125=head2 C<qw()> changes; new interpolating form 
    106126 
    107127    Was:    qw(foo) 
     
    115135operators.  See L<S03> for details. 
    116136 
     137Note that C<()> as a subscript is now a sub call, so instead of C<qw(a b)> you 
     138would write C<< qw<a b> >> or C<qw[a b]> (if you don't like plain C<< <a b> 
     139>>), that is). 
     140 
     141=head2 Other important operator changes 
     142 
     143String concatenation is now done with C<~>. 
     144 
     145Regex match is done with the smart match operator C<~~>, the perl 5 match 
     146operator C<=~> is gone. 
     147 
     148    if "abc" ~~ m/a/ { ... } 
     149 
     150C<|> and C<&> as infix operators now construct junctions. The binary AND and 
     151binary OR operators are split into string and numeric operators, that is C<~&> 
     152is binary string AND, C<+&> is binary numeric AND, C<~|> is binary string OR 
     153etc. 
     154 
     155Parenthesis don't construct lists any more, they merely group. Lists are 
     156constructed with the comma operator. It has looser precedence than the list 
     157assignment operator, which allows you to write lists on the right hand side 
     158wihtout parens: 
     159     
     160    my @list = 1, 2, 3;     # @list really has three elements 
     161 
     162The arrow operator C<< -> >> for dereferncing is gone. Since (nearly) 
     163everything is a reference, you can directly use the apropriate pair of 
     164parenthesis for either indexing or method calls: 
     165     
     166    my $lol = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; 
     167    say $lol[1][0];         # 4 
     168 
     169    my $s = sub { say "hi" }; 
     170    $s(); 
     171    # or 
     172    $s.(); 
     173    $lol.[1][0] 
     174 
    117175=cut 
    118176 
     
    128186    Now:    if  $a < $b  { ... } 
    129187 
    130 Likewise for C<while>, C<for>, etc. 
     188Likewise for C<while>, C<for>, etc. If you insist on using parens, make sure 
     189there's a space after the C<if>, otherwise it's a sub call. 
    131190 
    132191=head2 eval {} is now try {} 
     
    247306=head1 Objects 
    248307 
     308Perl 6 has a "real" object system, with key words for classes, methods and 
     309attributes. Public attributes have the C<.> twigil, private ones the C<!> 
     310twigil. 
     311 
     312    class YourClass { 
     313        has $!private; 
     314        has @.public; 
     315         
     316        # and with write accessor 
     317        has $.stuff is rw; 
     318 
     319        method do_something { 
     320            if self.can('bark') { 
     321                say "Something doggy"; 
     322            } 
     323        } 
     324    } 
     325 
    249326=head2 Method invocation changes from -> to . 
    250327 
     
    263340=head1 Overloading 
    264341 
     342Since both builtin functions and operators are multi subs and methods, 
     343changing their behaviour for particular types is a simple as adding the 
     344appropriate multi subs and methods. If you want these to be globally 
     345available, you have to but them into the C<GLOBAL> namespace, which is 
     346indicated by an asterisk. 
     347 
     348    multi sub *uc(TurkishStr $str) { ... } 
     349 
     350    # "overload" the string concatenation: 
     351    multi sub infix:<~>(TurkishStr $us, TurkishStr $them) { ... } 
     352 
     353If you want to offer a type cast to a particular type, just provide a method 
     354with the same name as the type you want to cast to. 
     355 
     356    sub needs_bar(Bar $x) { ... } 
     357    class Foo { 
     358        ... 
     359        # coercion to type Bar: 
     360        method Bar { ... } 
     361    } 
     362 
     363    needs_bar(Foo.new);         # coerces to Bar 
     364 
     365=head2 Offering Hash and List semantics 
     366 
     367If you want to write a class whose objects can be assigned to a variable with 
     368the C<@> sigil, you have to implement the C<Positional> roles. Likewise for 
     369the C<%> sigil you need to do the C<Associative> role. The C<&> sigil implies 
     370C<Callable>.  
     371 
     372The roles provides the operators C<< postcircumfix:<[ ]> >> (Positional; for 
     373array indexing), C<< postcircumfix:<{ }> >> 
     374(Associative) and C<< postcircumfix:<()> >> (Callable). The are technically 
     375just methods with a fancy syntax. 
     376You should override these to provide meaningful semantics. 
     377 
     378    class OrderedHash does Associative { 
     379        multi method postcircumfix:<{ }>(Int $index) { 
     380            # code for accessing single hash elements here 
     381        } 
     382        multi method postcircumfix:<{ }>(*@@slice) { 
     383            # code for accessing hash slices here 
     384        } 
     385        ... 
     386    } 
     387 
     388    my %orderedHash = OrderedHash.new(); 
     389    say %orderedHash{'a'}; 
     390 
     391See L<S13> for all the gory details. 
     392 
    265393=cut  
    266394 
     
    279407 
    280408#S26  
     409 
     410# XXX needs some brave rewrite 
    281411 
    282412=head1 Documentation 
     
    376506applicable. 
    377507 
    378  
     508In general you are encouraged to return objects that do the right thing in 
     509each possible context instead of asking for your context explicitly. 
    379510 
    380511=head1 Unfiled 
     
    413544Kirrily "Skud" Robert, <skud@cpan.org>, 
    414545Mark Stosberg, 
     546Moritz Lenz, 
    415547Trey Harris