Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Use 'ls' to read files in perl????

by AndyZaft (Hermit)
on Jul 07, 2010 at 19:31 UTC ( [id://848533]=note: print w/replies, xml ) Need Help??


in reply to Use 'ls' to read files in perl????

I always used this form:
my @files = <Data_files/*/Data.txt>;

Replies are listed 'Best First'.
Re^2: Use 'ls' to read files in perl????
by eyepopslikeamosquito (Archbishop) on Jul 08, 2010 at 08:07 UTC

    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:

    my @files = <*.pl>;
    to:
    Readonly my $FILE_PATTERN => '*.pl'; my @files = <$FILE_PATTERN>; # KABOOM! (probably)
    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.

    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).

      Yup, I figured glob is better, although I never read the PBP stuff before. Thank you for pointing it out and for the links, I like to learn how to use tools better. Even if only use them occasionally, it looks like I only use perl every decade or so. In any case I guess it was good to give him a different solution because this way we got an explanation why he should stick to glob :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://848533]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-24 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found