Just another Perl shrine | |
PerlMonks |
Re: To glob or not to globby haukex (Archbishop) |
on Jan 09, 2018 at 21:08 UTC ( [id://1207015]=note: print w/replies, xml ) | Need Help?? |
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.
In Section
Meditations
|
|