delirium has asked for the wisdom of the Perl Monks concerning the following question:
I can use a -e test for a single filename, and a glob test for a pattern. What I'm looking for is a single test to use if I don't know ahead of time if the string will refer to a single file or a shell pattern.
Running a few tests with perl one-liners gives me this:
$ ls *.txt javastuff.txt modules.txt $ perl -le '$,=" "; print glob "*.txt"' javastuff.txt modules.txt $ perl -le '$,=" "; print glob "joe.txt"' joe.txt $ perl -le '$,=" "; print glob "joe.txt*"' $ perl -le 'print ((-e "joe.txt")?"yes":"no")' no $ perl -le 'print ((-e "*.txt")?"yes":"no")' no $ perl -le 'print ((-e "modules.txt")?"yes":"no")' yes $
Note the false positive for joe.txt in the glob test, and the problem with *.txt in the -e test.
I am currently using a hack that appends an asterisk (*) to the string I'm testing, and testing it with a glob command, e.g.,
if ( @files = glob $pattern.'*' ) {...
but there is an edge case where I only want "file" when "file1" and "file2" also exist, and that needs to be accounted for.
Any suggestions? Thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
•Re: Test for file(s): glob or -e?
by merlyn (Sage) on Feb 18, 2004 at 13:15 UTC | |
by delirium (Chaplain) on Feb 18, 2004 at 18:37 UTC | |
|
Re: Test for file(s): glob or -e?
by inman (Curate) on Feb 18, 2004 at 14:50 UTC | |
by merlyn (Sage) on Feb 18, 2004 at 15:27 UTC | |
by inman (Curate) on Feb 18, 2004 at 18:46 UTC | |
|
Re: Test for file(s): glob or -e?
by ysth (Canon) on Feb 18, 2004 at 18:34 UTC | |
by Anonymous Monk on Feb 18, 2004 at 18:44 UTC | |
by ysth (Canon) on Feb 18, 2004 at 18:57 UTC |