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


In reply to Globbing drives me nuts - some things seem counterintuitive by Sandy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.