Revision 27503
- Date:
- 2009/07/10 23:11:48
- Files:
Legend:
- Added
- Removed
- Modified
-
docs/Perl6/Spec/S03-operators.pod
14 14 15 15 Created: 8 Mar 2004 16 16 17 Last Modified: 10 Jun 2009 18 Version: 168 17 Last Modified: 10 Jul 2009 18 Version: 169 19 19 20 20 =head1 Overview 21 21 … … 2215 2215 The filetest operators are gone. We now use a C<Pair> as a 2216 2216 pattern that calls an object's method: 2217 2217 2218 if $filename ~~ :e { say "exists" } 2218 if $filename.IO ~~ :e { say "exists" } 2219 2219 2220 2220 is the same as 2221 2221 2222 if $filename.e { say "exists" } 2222 if $filename.IO.e { say "exists" } 2223 2223 2224 2224 The 1st form actually translates to the latter form, so the object's 2225 2225 class decides how to dispatch methods. It just happens that 2226 C<Str> (filenames), C<IO> (filehandles), and C<Statbuf> (stat buffers) 2226 C<IO> (filehandles) and C<Statbuf> (stat buffers) 2227 2227 default to the expected filetest semantics, but C<$regex.i> might 2228 2228 tell you whether the regex is case insensitive, for instance. 2229 2229 … … 2245 2245 when :r & :w & :x 2246 2246 when all(:r,:w,:x) 2247 2247 2248 The advantage of the method form is that it can be used in places that 2249 require tighter precedence than C<~~> provides: 2248 The pair forms are useful only for boolean tests, so the 2249 method form must be used for any numeric-based tests: 2250 2250 2251 sort { $^a.M <=> $^b.M }, @files 2251 if stat($filename).s > 1024 {...} 2252 2252 2253 However, these still work: 2254 2255 given $fh { 2256 when :s {...} # file has size > 0 2257 when :!s {...} # file size == 0 2258 } 2259 2260 One advantage of the method form is that it can be used in places that 2261 require tighter precedence than C<~~> provides 2262 2263 sort { $^a.M <=> $^b.M }, @files».IO 2264 2253 2265 though that's a silly example since you could just write: 2254 2266 2255 sort { .M }, @files 2267 sort { .M }, @files».IO 2256 2268 2257 2269 But that demonstrates the other advantage of the method form, which is 2258 2270 that it allows the "unary dot" syntax to test the current topic. 2259 2271 2260 Unlike in earlier versions of Perl 6, these filetests do not return 2272 Unlike in earlier versions of Perl 6, these filetest methods do not return 2261 2273 stat buffers, but simple scalars of type C<Bool>, C<Int>, or C<Num>. 2262 2274 2263 2275 In general, the user need not worry about caching the stat buffer … … 2272 2284 object doesn't know its filename but does know its IO handle, then 2273 2285 C<.file> attempts to return C<.io.file>. 2274 2286 2275 Note that C<:s> still returns the filesize, but C<:!s> is true 2276 only if the file is of size 0, since it is smartmatched 2277 against the implicit False argument. By the same token, 2278 C<:s(0..1024)> will be true only for files of size 1K or less. 2279 2280 2287 (Inadvertent use of the Perl 5 forms will normally result in treatment 2281 2288 as a negated postdeclared subroutine, which is likely to produce an 2282 2289 error message at the end of compilation.) … … 3077 3084 Any Str string equality ~$_ eq X 3078 3085 3079 3086 Hash Pair test hash mapping $_{X.key} ~~ X.value 3080 Any Pair test object attribute ."{X.key}" ~~ X.value (e.g. filetests) 3087 Any Pair test object attribute ?."{X.key}" === ?X.value (e.g. filetests) 3081 3088 3082 3089 Set Set identical sets $_ === X 3083 3090 Hash Set hash keys same set $_.keys === X … … 3538 3545 Note that logical operators such as C<||> and C<^^> do not return a Bool, 3539 3546 but rather one of the operands. 3540 3547 3541 =head2 Reversed comparison operators 3548 =head2 Reversed operators 3542 3549 3543 3550 Any infix operator may be called with its two arguments reversed 3544 3551 by prefixing with C<R>. For instance, to do reversed comparisons: