in reply to Re: Use 'ls' to read files in perl????
in thread Use 'ls' to read files in perl????
Perl Best Practices, Chapter 8, "Built-in Functions", page 167: "Use glob, not <...>".
Conway argues that <> is overloaded with too many different meanings in Perl, leading to maintenance problems. After noting that changing:
to:my @files = <*.pl>;
blows up in your face, he concludes that "a construct that breaks when you attempt to improve its readability is, by definition, unmaintainable". Use glob and keep the angle brackets strictly for input operations.Readonly my $FILE_PATTERN => '*.pl'; my @files = <$FILE_PATTERN>; # KABOOM! (probably)
Update: Further to PBP, note Larry's sensible advice on language design:
Common operations should be "Huffman coded". That is, frequently used operators should be shorter than infrequently used ones.In this case, file input is a more common operation than file globbing and so deserves the <...> shortcut. This was proposed back in 2000 in rfc 34:
Angle brackets have two separate meanings in Perl - file globbing and line input from a filehandle. This means that perl attempts to Do What I Mean and often misinterprets me. Since file globbing can be accomplished with the glob function and since file input is the more common use of angle brackets, they should not be used for file globbing.Curiously, in Perl 6, <...> is no longer used for file input or file globbing, but as an improved version of Perl 5's qw operator. For file input, Perl 6 uses either $fh.readline(), or the shorter form =$fh. The Perl 5 built-in glob function is no longer built-in in Perl 6, but is now part of the IO modules (according to S29).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Use 'ls' to read files in perl????
by AndyZaft (Hermit) on Jul 08, 2010 at 13:43 UTC |