Sandy has asked for the wisdom of the Perl Monks concerning the following question:
I was writing a little sub that returned a list of files, given a particular file spec. It had to work in windows and unix. Sometimes wild cards are passed, sometimes not.
So... I used glob (not the actual code below, but the basic idea).
sub Get_File_List { my $pat = shift; return glob ($pat); } sub my_other_sub { my $file = shift; # --- maybe file has wildcards? Define array # to hold all files my @files = Get_File_List($file); # do stuff with @files }
Problem #1
If there are blanks in the file names, the array that is returned from Get_File_List will hold each part of the file name in a different array element...
i.e. input file name = "my dos file.txt"
gives $file[0]="my", $file[1]="dos" ... etc.
So I added code to my Get_File_List to handle the cases where the file names have spaces.
Merrily I go along, expecting that if the file spec does not find a file that matches, Get_File_List will return a null array. No such luck. Glob behaviour is shown below.
Imagine I have a directory with absolutely no files *.txt
Is this the normal behaviour of glob?? It is not what I expected, and yes I did read the manual.> perl -e 'print "result: <",glob("*.txt"),">\n";' result: <> > perl -e 'print "result: <",glob("xx.txt"),">\n";' result: <xx.txt> > dir > x.txt > perl -e 'print "result: <",glob("x.txt"),">\n";' result: <x.txt> > perl -e 'print "result: <",glob("*.txt"),">\n";' result: <x.txt> > perl -e 'print "result: <",glob("xx.txt"),">\n";' result: <xx.txt> > dir xx.txt xx.txt: No such file or directory
Maybe I should use readdir instead, and switch "*.txt" to ".*\.txt"??
Sandy
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Globbing drives me nuts - some things seem counterintuitive
by dragonchild (Archbishop) on Apr 01, 2004 at 21:22 UTC | |
by Sandy (Curate) on Apr 01, 2004 at 21:40 UTC | |
|
Re: Globbing drives me nuts - some things seem counterintuitive
by Roy Johnson (Monsignor) on Apr 01, 2004 at 21:09 UTC | |
|
Re: Globbing drives me nuts - some things seem counterintuitive
by Abigail-II (Bishop) on Apr 01, 2004 at 22:06 UTC | |
by Sandy (Curate) on Apr 01, 2004 at 22:09 UTC | |
by QM (Parson) on Apr 01, 2004 at 23:15 UTC |