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!