Sandy has asked for the wisdom of the Perl Monks concerning the following question:

Am I nuts?

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

> 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
Is this the normal behaviour of glob?? It is not what I expected, and yes I did read the manual.

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
    I'd look at the modules that are built to be OS-independent. Modules like IO::Dir, File::Spec, File::Find ... don't roll your own when a most excellent blount has already been rolled for you. :-)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      thanks...
Re: Globbing drives me nuts - some things seem counterintuitive
by Roy Johnson (Monsignor) on Apr 01, 2004 at 21:09 UTC
    Is this the normal behaviour of glob??
    Yes. It doesn't always read the directory; only when it has to evaluate wildcards. In fact, if you put alternators (e.g., foo.{bar,baz}), it will generate all the combinations for you without looking to see whether they exist as files.

    I'm not qualified to tell you whether you're nuts. :-)


    The PerlMonk tr/// Advocate
Re: Globbing drives me nuts - some things seem counterintuitive
by Abigail-II (Bishop) on Apr 01, 2004 at 22:06 UTC
    Imagine I have a directory with absolutely no files *.txt
    > perl -e 'print "result: <",glob("*.txt"),">\n";' result: <x.txt>
    That's a bug. And it's something I can't reproduce.

    Abigail

      Sorry Abigail

      If you look at all of the stuff, in between I made a file  dir >x.txt just to prove (to myself) that it actually worked when there was a file.

      What drives me nuts is it returns "xx.txt" if "xx.txt" was passed to the "glob" even if "xx.txt" does not exist.

        What drives me nuts is it returns "xx.txt" if "xx.txt" was passed to the "glob" even if "xx.txt" does not exist.
        While that can be frustrating if it doesn't DWIM, I can think of a use for it.
        foreach my $file_spec ( @ARGV ) { my @files = glob( $file_spec ); foreach my $file ( @files ) { if ( not(-e $file) and ( $file =~ /[.*]/ ) ) # change as appropriate { warn "Error: file $f doesn't exist," . " or wildcard didn't match, " unless -e $file; } } }
        You might want to know which wildcard didn't match.

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of