Changeset 9958 for docs/Perl6/FAQ

Show
Ignore:
Timestamp:
04/15/06 15:24:04 (3 years ago)
Author:
audreyt
Message:

* Perl6::FAQ::Capture - PODification.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • docs/Perl6/FAQ/Capture.pod

    r9957 r9958  
    1 = Q: What happened to references? 
    2  
    3 A: They have been superseded by Captures. 
    4  
    5  
    6 = Q: What is a Capture? 
     1=head1 NAME 
     2 
     3Perl6::FAQ::Capture - Capture objects 
     4 
     5=head1 DESCRIPTION 
     6 
     7This FAQ answers questions about B<Capture> objects, as well as their uses in 
     8bindings and function calls. 
     9 
     10=head2 What happened to references? 
     11 
     12They have been superseded by Capture objects. 
     13 
     14=head2 What is a Capture? 
    715 
    816A Capture is an object representing arguments in a function call. 
    917 
    10 For example, the arguments in say("Hello", "World") is a Capture object 
    11 denoted by \("Hello", "World"). 
    12  
    13  
    14 = Q: Can I see how captures look like? Does \$x no longer work? 
    15  
    16 Prefix \ is now the Capture constructor.  These are all equivalent: 
     18For example, the arguments in C<say("Hello", "World")> is a Capture object 
     19denoted by C<\("Hello", "World")>. 
     20 
     21=head2 Can I see how captures look like? Does \$x no longer work? 
     22 
     23Prefix C<\> is now the Capture constructor.  These are all equivalent: 
    1724 
    1825  my $xcap1 = \$x; 
     
    2027  my $xcap3 = \($x :); 
    2128 
    22 All three represent an argument list with a single invocant $x, and hence are 
     29All three represent an argument list with a single invocant C<$x>, and hence are 
    2330all equivalent with each other: 
    2431 
    2532  $xcap1 === $xcap2 === $xcap3; # true 
    2633 
    27  
    28 = Q: What about capturing multiple arguments? 
     34=head2 What about capturing multiple arguments? 
    2935 
    3036Here's how Captures look like deferred argument lists: 
     
    4248  say "It has $car_cap<doors> doors" # 2 doors 
    4349 
    44  
    45 = Q: How do I extract all positional arguments or all named arguments? 
     50=head2 How do I extract all positional arguments or all named arguments? 
    4651 
    4752As you can see above, a Capture object holds both positional and named parts. 
     
    5257 
    5358Both forms flatten under list context, so they may be used interchangeably. 
    54 However, only the postfix .[] form interpolates in a string. 
    55  
    56 Similarly, access to named arguments is available by treating the Capture as a hash: 
     59However, only the postfix C<.[]> form interpolates in a string. 
     60 
     61Similarly, access to named arguments is available by treating the Capture 
     62as a hash: 
    5763 
    5864  $cap{};  # postfix .{} 
     
    6167Note that the positional parts do not include the invocant. 
    6268 
    63 = Q: What do you mean by "invocant"? 
    64  
    65 When you call a object's method, the "self" inside the method is set to that 
     69=head2 What do you mean by "invocant"? 
     70 
     71When you call a object's method, the C<self> inside the method is set to that 
    6672object. These are all equivalent: 
    6773 
     
    8389  $x === $(\$x); 
    8490 
    85  
    86 = Q: What about multimethod dispatch and cases where there is more than one invocant? 
     91=head2 What about multimethod dispatch and cases where there is more than one invocant? 
    8792 
    8893There's always one invocant, but it may be an Array of several values (see below). 
    8994 
    90  
    91 = Q: What happens when a named argument is repeated? 
     95=head2 What happens when a named argument is repeated? 
    9296 
    9397Let's look at some examples: 
     
    100104 
    101105A Capture object may hold named arguments that occur twice or more.  When it's 
    102 bound to a variable in the Signature, if the sigil is "@", then it expands to a 
    103 list of all arguments (in the order they were specified).  Otherwise (i.e. if 
    104 it's bound to a scalar or a slurpy hash), the last argument overrides the 
    105 previous ones. 
    106  
    107  
    108 = Q: What do *$x, *@x and *%x mean? 
    109  
    110 The prefix * method casts an object into a Capture object, and merges it 
     106bound to a variable in the Signature, if the sigil is C<@>, then it expands to a 
     107list of all arguments (in the order they were specified). 
     108 
     109Otherwise (i.e. if it's bound to a scalar or a slurpy hash), the last argument 
     110overrides the previous ones. 
     111 
     112=head2 What do *$x, *@x and *%x mean? 
     113 
     114The prefix C<*> method casts an object into a Capture object, and merges it 
    111115into the Capture being currently constructed (e.g. an argument list): 
    112116 
     
    121125    my $p = (x => 42);  f(*$p);     # f(x => 42) 
    122126 
    123  
    124 = Q: What does $x = \@y mean? 
    125  
    126 The same as $x = (@y: ). That is, a Capture of one array as invocant.  
     127=head2 What does $x = \@y mean? 
     128 
     129The same as C<$x = (@y: )>. That is, a Capture of one array as invocant.  
    127130 
    128131Unlike in Perl 5, this means that you can get back the Array object with: 
     
    139142   
    140143Note the need for the extra $ sigil, implying we are accessing the captured invocant. 
    141 @$x.push() would mean an attempt to add an extra positional argument into $x; however, 
    142 this is forbidden by the spec, as all parts are immutable in an Capture object. 
     144C<@$x.push()> would mean an attempt to add an extra positional argument into 
     145C<$x>; this would fail as all parts are immutable in an Capture object. 
    143146 
    144147(Captures are immutable; their underlying data may not be.) 
    145148 
    146  
    147 = Q: So $x = @y does not mean $x = \@y anymore. What does it mean then? 
    148  
    149 Assign $x with the object @y is bound to (typically an Array object). 
    150  
    151 This does not create Capture objects; to get back @y, @$x would do. 
     149=head2 So $x = @y does not mean $x = \@y anymore. What does it mean then? 
     150 
     151It means assigning C<$x> with the object bound to C<@y> (typically an Array object). 
     152 
     153This does not create Capture objects; to get back C<@y>, C<@$x> would do. 
    152154 
    153155Also note that all these forms mean the same thing: 
     
    160162  @y[][][]; 
    161163 
    162 = Q: Does this mean I can't have something akin to a refrence to a reference? 
     164=head2 Does this mean I can't have something akin to a refrence to a reference? 
    163165 
    164166You can, as a Capture can contain another capture object in its invocant slot: 
     
    167169    say $$$x; # same as "say 3" 
    168170 
    169 = Q: Can I create a "pass-through" function that captures all arguments? 
    170  
    171 Yes, using the \$ signature: 
     171=head2 Can I create a "pass-through" function that captures all arguments? 
     172 
     173Yes, using the C<\$> signature: 
    172174 
    173175    sub f (\$args) { g(*$args) } 
    174176    f(1, 2, x => 42);   # same as g(1, 2, x => 42) 
    175177 
    176 The $args above becomes a Capture object. 
    177  
    178 = Q: How is @x = (1, 2, 3) different from @y := (1, 2, 3) ? 
    179  
    180 The latter is an error. :-)  The := operator binds its left hand side (a Signature object) 
    181 to its right hand side (a Capture object), so the latter form is akin to: 
     178The C<$args> above becomes a Capture object. 
     179 
     180=head2 How is @x = (1, 2, 3) different from @y := (1, 2, 3) ? 
     181 
     182The latter is an error. :-) 
     183 
     184The C<:=> operator binds its left hand side (a Signature object) to its right 
     185hand side (a Capture object), so the latter form is akin to: 
    182186 
    183187  sub foo (@y is rw) { ... } 
     
    193197But they are still different from the assignment form. 
    194198 
    195  
    196 = Q: How is @x = (1, 2, 3) different from *@y := (1, 2, 3), then? 
    197  
    198 This means that while @y holds the values 1, 2, and 3, you cannot modify the 
     199=head2 How is @x = (1, 2, 3) different from *@y := (1, 2, 3), then? 
     200 
     201This means that while C<@y> holds the values 1, 2, and 3, you cannot modify the 
    199202container itself, so this won't work: 
    200203 
     
    207210  my @x;        # implicitly does the same thing 
    208211 
    209 so @x = (1, 2, 3) would simply populate the previously allocated Array object 
     212so C<@x = (1, 2, 3)> would simply populate the previously allocated Array object 
    210213with new elements. 
    211214 
    212  
    213 = Q: Is there any difference between $x = (1, 2, 3) and @y = (1, 2, 3)? 
     215=head2 Is there any difference between $x = (1, 2, 3) and @y = (1, 2, 3)? 
    214216 
    215217Yes.  The right-hand side in both case is a single List object constructed with 
    216 the list-associative infix:<,> operator, but it is flattened in the second 
     218the list-associative C<< infix:<,> >> operator, but it is flattened in the second 
    217219case, and its elements are put into the previously allocated Array container 
    218 bound to @y: 
     220bound to C<@y>: 
    219221 
    220222    $x.push(0); # error: cannot find method: List.push 
    221223    @y.push(0); # works just fine 
    222224 
    223  
    224 = Q: Is there any difference between $x = [1, 2, 3] and @y = [1, 2, 3]? 
    225  
    226 Yes.  As in Perl 5, the Array constructor [...] does not flatten under list 
    227 context, so @y receives a List with one element (an Array object), which then 
    228 becomes @y[0]: 
     225=head2 Is there any difference between $x = [1, 2, 3] and @y = [1, 2, 3]? 
     226 
     227Yes.  As in Perl 5, the Array constructor C<circumfix:[ ]> does not flatten 
     228under list context, so C<@y> receives a List with one element (an Array 
     229object), which then becomes C<@y[0]>: 
    229230 
    230231    $x.elems; # 3 
     
    237238    @y.push(0);     # works - @y.elems becomes 2  
    238239 
    239  
    240 = Q: Is there any difference between $x := [1, 2, 3] and @y := [1, 2, 3]? 
     240=head2 Is there any difference between $x := [1, 2, 3] and @y := [1, 2, 3]? 
    241241 
    242242For the usual method-based operations, they are pretty much interchangeable: