RFC 320 (v1) Allow grouping of -X file tests and add C<filetest> builtin
[email protected] (Perl6 RFC Librarian) 26 Sep 2000 05:53:13 -0000
| Newsgroups | perl.perl6.announce.rfc,perl.perl6.language |
|---|---|
| Message-ID | <[email protected]> |
This and other RFCs are available on the web at http://dev.perl.org/rfc/ =head1 TITLE Allow grouping of -X file tests and add C<filetest> builtin =head1 VERSION Maintainer: Nathan Wiger <[email protected]> Date: 25 Sep 2000 Mailing List: [email protected] Number: 320 Status: Developing =head1 ABSTRACT Currently, file tests cannot be grouped, resulting in very long expressions when one wants to check to make sure some thing is a readable, writeable, executable directory: if ( -d $file && -r $file && -w $file && -x $file ) { ... } It would be really nice if these could be grouped instead: if ( -drwx $file ) { ... } Notice how much easier this is to read and write. =head1 DESCRIPTION =head2 File Test Grouping See above. Multiple file tests, when grouped, should be ANDed together. This RFC does not propose a way to OR them, since usage like this: if ( -d $file || -r $file || -w $file || -x $file ) { ... } Is highly uncommon, to say the least. Notice this has the nice side effect of eliminating the need for C<_> in many cases, since this: if ( -d $file && -r _ && -w _ && -x _ ) { ... } Can simply be written as a single grouped file test, as shown above. If you need to check for more complex logic, you still have to do that separately: if ( -drwx $file and ! -h $file ) { ... } This is the simplest and also probably the clearest way to implement this. =head2 New C<filetest> Builtin This RFC also proposes a new C<filetest> builtin that is actually what is used for these tests. The C<-[a-zA-Z]+> form is simply a shortcut to this builtin, just like <> is a shortcut to C<readline>. So: if ( -rwdx $file ) { ... } Is really just a shortcut to the C<filetest> builtin: if ( filetest $file, 'rwdx' ) { ... } Either form could be used, depending on the user's preferences (just like C<readline>). =head1 IMPLEMENTATION This would involve making C<-[a-zA-Z]+> a special token in all contexts, serving as a shortcut for the C<filetest> builtin. =head1 MIGRATION There is a subtle trap if you are negating subroutines: $result = -drwx $file; And expect this to be parsed like this: $result = - &drwx($file); However, usage such as this is exceedingly unlikely, and can simply be resolved by the p52p6 translator looking for C<-([a-zA-Z]{2,})> and replacing it with C<- $1>, since injecting a single space will break up the token. =head1 REFERENCES This grew out of a discussion on RFC 290 between myself, John Allen, Clayton Scott, Bart Lateur, and others