I have a dedicated directory where users drop files. I have a config file that will take action upon the files in the directory based on the file name. Sometimes the users will drop a file name with extra characters in the file name. For example, file name "abc123" might be named "abc123.txt" or abc123_Jan2010" when they place the file tomorrow.
In my config file, I would like to enter fixed file names like "abc123" or filenames that need to be expanded, like "abc*". I want the fixed filename to match the exact file only, and the the expanded filename to match any file that fits the pattern. For example, "abc123" would match file name abc123 only, while "abc*" would match all the examples above.
I have tried using glob in my example below. My target directory has the following files:
> ls -1 /tmp/test
abc123
abc123.txt
abc123_Jan2010
Here is my code:
#!/opt/perl5/current/bin/perl
chdir "/tmp/test";
$CONFIG = "abc123*";
@files = glob("$CONFIG");
foreach $file (@files) {
print "$file \n";
}
Which provides the results I would expect:
> ./test.pl
abc123
abc123.txt
abc123_Jan2010
This works fine as long as the $CONFIG includes a wildcard. If I change the config to something like
$CONFIG = "xyz789";
the results are:
> ./test.pl
xyz789
Since the $CONFIG does not match a file in the directory, I would expect to get no results.
Is glob the wrong choice? Is there something that would expand the pattern match if a wildcard was present, and not expand if no wild card exists?
Thanks for any help you can provide!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.