Revision 27755
- Date:
- 2009/07/27 18:54:44
- Files:
Legend:
- Added
- Removed
- Modified
-
docs/Perl6/Spec/S06-routines.pod
2090 2090 .context 2091 2091 .caller 2092 2092 .leave 2093 .want 2094 2093 .inline 2095 2094 .package 2096 2095 .file … … 2101 2100 The C<.context> and C<.caller> methods work the same as the functions 2102 2101 except that they are relative to the context supplied as invocant. 2103 2102 The C<.leave> method can force an immediate return from the 2104 specified context. The C<.want> method returns known smart-matchable 2105 characteristics of the specified context. 2103 specified context. 2106 2104 2107 2105 The C<.inline> method says whether this block was entered implicitly 2108 2106 by some surrounding control structure. Any time you invoke a block or … … 2124 2122 2125 2123 =head2 The C<want> function 2126 2124 2127 The C<want> function returns a C<Signature> object that contains 2128 information about the context in which the current block, closure, 2129 or subroutine was called. The C<want> function is really just 2130 short for C<caller.want>. (Note that this is what your current 2131 routine's caller wants from your routine, not necessarily the same as 2132 C<context.want> when you are embedded in a block within a subroutine. 2133 Use C<context.want> if that's what you want.) 2125 The C<want> function is gone. If you want context specific behavior, 2126 return an object instead that responds accordingly to the various contextual 2127 methods. 2134 2128 2135 As with normal function signatures, you can test the result of C<want> with a 2136 smart match (C<~~>) or a C<when>: 2129 (Conjecture: in future we might want to provide some syntactic sugar that 2130 makes it easier to create such objects. Or maybe a type that takes values 2131 or code references for the various contexts, so that you can write 2137 2132 2138 given want { 2139 when :($) {...} # called in item context 2140 when :(*@) {...} # called in list context 2141 when :($ is rw) {...} # expected to return an lvalue 2142 when :($,$) {...} # expected to return two values 2143 ... 2144 } 2133 return ContextProxy.new: 2134 Int => 3, 2135 item => { @.list.join(', ') }, 2136 liste => { ... }, 2137 ; 2138 or something similar.) 2145 2139 2146 Or use its shorthand methods to reduce line noise: 2147 2148 if want.item {...} # called in non-lvalue item context 2149 elsif want.list {...} # called in list context 2150 elsif want.void {...} # called in void context 2151 elsif want.rw {...} # expected to return an lvalue 2152 2153 The C<.arity> and C<.count> methods also work here: 2154 2155 if want.arity > 2 {...} # must return more than two values 2156 if want.count > 2 {...} # can return more than two values 2157 2158 Their difference is that C<.arity> considers only mandatory parts, 2159 while C<.count> considers also optional ones, including C<*$>: 2160 2161 ($x, $y) = f(); # Within &f, want === :(*$?, *$?, *@) 2162 # want.arity === 0 2163 # want.count === 2 2164 2165 2140 =head2 The C<leave> function 2166 2141 2167 2142 As mentioned above, a C<return> call causes the innermost surrounding -
docs/Perl6/Spec/S29-functions.pod
250 250 251 251 See C<Synopsis 17: Concurrency> for more details. 252 252 253 =item want 254 255 See L<S06/The C<want> function>. 256 257 253 =item die 258 254 259 255 =item fail