Changeset 13610 for docs/Perl6/FAQ
- Timestamp:
- 09/25/06 13:04:55 (2 years ago)
- Files:
-
- 1 modified
-
docs/Perl6/FAQ/Capture.pod (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
docs/Perl6/FAQ/Capture.pod
r11830 r13610 53 53 54 54 # passing the opaque Capture as actual function arguments 55 my $car = make_car( [,]=$car_cap);55 my $car = make_car(|$car_cap); 56 56 57 57 # extracting a specific positional argument … … 161 161 argument overrides the previous ones. 162 162 163 =head3 What do [,]$x, [,]@x and [,]%x mean?164 165 The prefix C< [,]> operator casts objects into Captures, and merges them163 =head3 What do |$x, |@x and |%x mean? 164 165 The prefix C<|> operator casts objects into Captures, and merges them 166 166 t into the Capture currently being constructed (e.g. an argument list): 167 167 168 168 my $cap = \(1, 2, x=>42); 169 f( [,] =$cap); # f(1, 2, x=>42)169 f(|$cap); # f(1, 2, x=>42) 170 170 171 171 Array, List, Hash and Pair objects cast into Capture objects in obvious 172 172 ways: 173 173 174 my $a = [1, 2]; f( [,]@$a); # f(1, 2)175 my $l = (1, 2); f( [,]@$l); # f(1, 2)176 my $h = {x => 42}; f( [,]%$h); # f(x => 42)177 my $p = (x => 42); f( [,]%$p); # f(x => 42)174 my $a = [1, 2]; f(|@$a); # f(1, 2) 175 my $l = (1, 2); f(|@$l); # f(1, 2) 176 my $h = {x => 42}; f(|%$h); # f(x => 42) 177 my $p = (x => 42); f(|%$p); # f(x => 42) 178 178 179 179 It also works with C<@> and C<%> variables: 180 180 181 my @a = (1, 2); f( [,]@a); # f(1, 2)182 my @a := [1, 2]; f( [,]@a); # f(1, 2)183 my %h = (x => 42); f( [,]%a); # f(x => 42)184 my %h := {x => 42}; f( [,]%h); # f(x => 42)181 my @a = (1, 2); f(|@a); # f(1, 2) 182 my @a := [1, 2]; f(|@a); # f(1, 2) 183 my %h = (x => 42); f(|%a); # f(x => 42) 184 my %h := {x => 42}; f(|%h); # f(x => 42) 185 185 186 186 … … 236 236 Yes, using the C<\$> signature: 237 237 238 sub f (\$args) { g( [,] =$args) }238 sub f (\$args) { g(|$args) } 239 239 f(1, 2, x => 42); # same as g(1, 2, x => 42) 240 240 … … 248 248 249 249 method front_meth { 250 $!real_obj.back_meth( [,]@_, %_ );250 $!real_obj.back_meth( |<< @_, %_ ); 251 251 } 252 252 … … 255 255 256 256 method front_meth (\$args) { 257 $!real_obj_A.back_meth( [,] =$args );257 $!real_obj_A.back_meth( |$args ); 258 258 } 259 259 … … 349 349 In the context of rules, Captures are superclasses of Match. So in the example of: 350 350 351 my $rv = ("x-y-z-moose" ~~ /(.)-(.)-(.)-<animal> :{ return give_birth( [,] =$/) })351 my $rv = ("x-y-z-moose" ~~ /(.)-(.)-(.)-<animal> :{ return give_birth(|$/) }) 352 352 353 353 C<give_birth()> gets called with 'x', 'y', and 'z' as positional arguments,
