Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: To glob or not to glob

by haukex (Archbishop)
on Jan 09, 2018 at 21:08 UTC ( [id://1207015]=note: print w/replies, xml ) Need Help??


in reply to To glob or not to glob

Thank you all very much for the replies so far! (I've /msg'ed everyone to make you aware of this reply-to-all.)

Laurent_R, Eily, and morgon, thank you for your thoughts on recommending glob. I'm not sure if there is one true answer as to whether glob can generally be recommended or not - when used properly, it provides nice and short code, while on the other hand, the various caveats can be easily forgotten, and later refactoring of the code could introduce problems, e.g. when a formerly static pattern gets a variable interpolated into it. I guess my emphasis should have been a bit more strong: "it should not be blindly used or recommended."

I would probably say that glob with fixed strings is generally fine, but interpolating variables (especially user-supplied paths at the beginning of the pattern) can get very tricky, and I personally often reach for Path::Class in these cases. Another situation in which glob is probably fine is when the user supplies the entire pattern, e.g. as part of a configuration file ("files = ~/foo/*.log"), and one then does @files = glob($config{files}). One can simply point to the documentation of glob and File::Glob when documenting that configuration option. (The problem of someone overriding glob globally remains, although I wonder how often that might happen...)

choroba, thank you for the additional caveat, which I've added to the root node. Now that you mention it, I dimly remember this being discussed before, and a bit of super searching found this thread with the reference to RT#123404 - I should have remembered this!

karlgoethebier, you are quite correct that those modules can also be limited to just one directory, thank you. It feels like a little bit of overkill to use a module like those for a single directory, but then again, it's good to use the tools one knows. My comment was hinting at the fact that I've seen modules like File::Find etc. recommended as an alternative to readdir without mentioning their recursive behavior.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-23 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found