Changeset 9958 for docs/Perl6/FAQ
- Timestamp:
- 04/15/06 15:24:04 (3 years ago)
- Files:
-
- 1 modified
-
docs/Perl6/FAQ/Capture.pod (modified) (14 diffs)
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 3 Perl6::FAQ::Capture - Capture objects 4 5 =head1 DESCRIPTION 6 7 This FAQ answers questions about B<Capture> objects, as well as their uses in 8 bindings and function calls. 9 10 =head2 What happened to references? 11 12 They have been superseded by Capture objects. 13 14 =head2 What is a Capture? 7 15 8 16 A Capture is an object representing arguments in a function call. 9 17 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: 18 For example, the arguments in C<say("Hello", "World")> is a Capture object 19 denoted by C<\("Hello", "World")>. 20 21 =head2 Can I see how captures look like? Does \$x no longer work? 22 23 Prefix C<\> is now the Capture constructor. These are all equivalent: 17 24 18 25 my $xcap1 = \$x; … … 20 27 my $xcap3 = \($x :); 21 28 22 All three represent an argument list with a single invocant $x, and hence are29 All three represent an argument list with a single invocant C<$x>, and hence are 23 30 all equivalent with each other: 24 31 25 32 $xcap1 === $xcap2 === $xcap3; # true 26 33 27 28 = Q: What about capturing multiple arguments? 34 =head2 What about capturing multiple arguments? 29 35 30 36 Here's how Captures look like deferred argument lists: … … 42 48 say "It has $car_cap<doors> doors" # 2 doors 43 49 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? 46 51 47 52 As you can see above, a Capture object holds both positional and named parts. … … 52 57 53 58 Both 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: 59 However, only the postfix C<.[]> form interpolates in a string. 60 61 Similarly, access to named arguments is available by treating the Capture 62 as a hash: 57 63 58 64 $cap{}; # postfix .{} … … 61 67 Note that the positional parts do not include the invocant. 62 68 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 that69 =head2 What do you mean by "invocant"? 70 71 When you call a object's method, the C<self> inside the method is set to that 66 72 object. These are all equivalent: 67 73 … … 83 89 $x === $(\$x); 84 90 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? 87 92 88 93 There's always one invocant, but it may be an Array of several values (see below). 89 94 90 91 = Q: What happens when a named argument is repeated? 95 =head2 What happens when a named argument is repeated? 92 96 93 97 Let's look at some examples: … … 100 104 101 105 A 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 a103 list of all arguments (in the order they were specified). Otherwise (i.e. if104 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 it106 bound to a variable in the Signature, if the sigil is C<@>, then it expands to a 107 list of all arguments (in the order they were specified). 108 109 Otherwise (i.e. if it's bound to a scalar or a slurpy hash), the last argument 110 overrides the previous ones. 111 112 =head2 What do *$x, *@x and *%x mean? 113 114 The prefix C<*> method casts an object into a Capture object, and merges it 111 115 into the Capture being currently constructed (e.g. an argument list): 112 116 … … 121 125 my $p = (x => 42); f(*$p); # f(x => 42) 122 126 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 129 The same as C<$x = (@y: )>. That is, a Capture of one array as invocant. 127 130 128 131 Unlike in Perl 5, this means that you can get back the Array object with: … … 139 142 140 143 Note 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.144 C<@$x.push()> would mean an attempt to add an extra positional argument into 145 C<$x>; this would fail as all parts are immutable in an Capture object. 143 146 144 147 (Captures are immutable; their underlying data may not be.) 145 148 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 151 It means assigning C<$x> with the object bound to C<@y> (typically an Array object). 152 153 This does not create Capture objects; to get back C<@y>, C<@$x> would do. 152 154 153 155 Also note that all these forms mean the same thing: … … 160 162 @y[][][]; 161 163 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? 163 165 164 166 You can, as a Capture can contain another capture object in its invocant slot: … … 167 169 say $$$x; # same as "say 3" 168 170 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 173 Yes, using the C<\$> signature: 172 174 173 175 sub f (\$args) { g(*$args) } 174 176 f(1, 2, x => 42); # same as g(1, 2, x => 42) 175 177 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: 178 The C<$args> above becomes a Capture object. 179 180 =head2 How is @x = (1, 2, 3) different from @y := (1, 2, 3) ? 181 182 The latter is an error. :-) 183 184 The C<:=> operator binds its left hand side (a Signature object) to its right 185 hand side (a Capture object), so the latter form is akin to: 182 186 183 187 sub foo (@y is rw) { ... } … … 193 197 But they are still different from the assignment form. 194 198 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 201 This means that while C<@y> holds the values 1, 2, and 3, you cannot modify the 199 202 container itself, so this won't work: 200 203 … … 207 210 my @x; # implicitly does the same thing 208 211 209 so @x = (1, 2, 3)would simply populate the previously allocated Array object212 so C<@x = (1, 2, 3)> would simply populate the previously allocated Array object 210 213 with new elements. 211 214 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)? 214 216 215 217 Yes. 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 second218 the list-associative C<< infix:<,> >> operator, but it is flattened in the second 217 219 case, and its elements are put into the previously allocated Array container 218 bound to @y:220 bound to C<@y>: 219 221 220 222 $x.push(0); # error: cannot find method: List.push 221 223 @y.push(0); # works just fine 222 224 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 227 Yes. As in Perl 5, the Array constructor C<circumfix:[ ]> does not flatten 228 under list context, so C<@y> receives a List with one element (an Array 229 object), which then becomes C<@y[0]>: 229 230 230 231 $x.elems; # 3 … … 237 238 @y.push(0); # works - @y.elems becomes 2 238 239 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]? 241 241 242 242 For the usual method-based operations, they are pretty much interchangeable:
