Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

A new teammate demonstrated something like the following at the beginning of a script.

use strict; use warnings; our @arr = <ABC DEF GHI>;

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:
1. my @many = glob "{apple,tomato,cherry}={green,yellow,red}";

I created the following to try to show why it's a bad idea.

use strict; use warnings; use Data::Dumper; my @arr1 = < abc def ghi f* >; my @arr2 = qw( abc def ghi f* ); my @arr3 = glob('abc def ghi z*'); print Dumper(\@arr1); print Dumper(\@arr2); print Dumper(\@arr3); __DATA__ $VAR1 = [ 'abc', 'def', 'ghi', 'file1.txt', 'file2.txt' ]; $VAR1 = [ 'abc', 'def', 'ghi', 'f*' ]; $VAR1 = [ 'abc', 'def', 'ghi' ];

perl -MO=Deparse glob_to_array.pl produces the following.

use Data::Dumper; use File::Glob (); use warnings; use strict 'refs'; my(@arr1) = glob(' abc def ghi f* '); my(@arr2) = ('abc', 'def', 'ghi', 'f*'); my(@arr3) = glob('abc def ghi z*'); print Dumper(\@arr1); print Dumper(\@arr2); print Dumper(\@arr3); glob_to_array.pl syntax OK

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


In reply to Creating arrays with glob patterns without metacharacters by Lotus1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-19 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found