|We don't bite newbies here... much|
Creating arrays with glob patterns without metacharactersby Lotus1 (Curate)
|on Jan 05, 2018 at 15:51 UTC||Need Help??|
Lotus1 has asked for the
wisdom of the Perl Monks concerning the following question:
A new teammate demonstrated something like the following at the beginning of a script.
I'm going to go over when to use our but I'm struggling with what to say about using glob like this. It works as long as there isn't a metacharacter other than curly braces. He has been told already about the use of qw() to create an array like this but I suspect he copied this from somewhere. In the perldocs for glob I found this:
If non-empty braces are the only wildcard characters used in the glob, no filenames are matched, but potentially many strings are returned. For example, this produces nine strings, one for each pairing of fruits and colors:
I created the following to try to show why it's a bad idea.
perl -MO=Deparse glob_to_array.pl produces the following.
Deparse shows that qw() does the job without calling glob and risking something unexpected if the text happens to contain a metacharacter (other than curly braces). The best thing I can come up with is for a program that the team will need to support this is a bad idea since it could cause unintended and confusing side effects. Suggestions for better demonstrations or documentation would be appreciated. Maybe I'm being too critical and should lower my critic setting.
Note: I changed the title