Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Isn't glob annoying? (somehow)

by PetaMem (Priest)
on Aug 02, 2002 at 08:35 UTC ( [id://187019]=perlquestion: print w/replies, xml ) Need Help??

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

Hello brothers,

I've stumbled across the following code:

my $mask = shift; my @folders = glob("$mask"); if (scalar(@folders) == 0) { return undef; }
It seems unelegant and unrobust to me, so I investigated a little bit further. To my surprise I found a strange behaviour of glob, when a glob is given that does not match but that does not contain wildcards also. Given the folowing examples:
@t1 = glob('*1'); @t2 = glob('1'); @t3 = glob('file2'); @t4 = glob('*3');
assume you have the files file1 and file2 in the directory glob is searching, the returns are 'file1' for @t1, '1' for @t2, 'file2' for @t3 and the empty list for @t4.

The behaviour for @t2 is what seems odd/annoying. In my opinion it should be empty, as the current semantics make it indistinguishable from the @t3 case - which is correct imho.

Please tell me, that I've found a bug that is soon to be fixed... :-}

Bye
 PetaMem

Replies are listed 'Best First'.
Re: Isn't glob annoying? (somehow)
by dada (Chaplain) on Aug 02, 2002 at 11:07 UTC
    as already pointed out, glob isn't strictly supposed to find existing files, but rather to do filenames expansion. however, you can do this to get the result you expect:

    @t1 = grep(-e, glob('*1')); @t2 = grep(-e, glob('1')); @t3 = grep(-e, glob('file2')); @t4 = grep(-e, glob('*3'));

    cheers,
    Aldo

    __END__ $_=q,just perl,,s, , another ,,s,$, hacker,,print;
Re: Isn't glob annoying? (somehow)
by Courage (Parson) on Aug 02, 2002 at 08:49 UTC
    perldoc -f glob says:
    glob EXPR glob Returns the value of EXPR with filename expansions such as + the standard Unix shell /bin/csh would do. This is the interna +l function implementing the "<*.c>" operator, but you can us +e it directly. If EXPR is omitted, "$_" is used. The "<*.c>" op +erator is discussed in more detail in the section on "I/O Operato +rs" in the perlop manpage. ....
    As shell expansion, this does what it promises. Here is an example excerpt from my /bin/sh on cygwin:
    $ echo * Desktop Mail _viminfo building-gcc-for-mips.txt car.pic gcc gdbtk.ini +kbuildsycoca.exe.stackdump kikbd.exe.stackdump parrot pwin32 x0 x1 xx Administrator@RTC ~ $ echo *jd *jd Administrator@RTC ~ $ echo dwq dwq

    Courage, the Cowardly Dog.

      First off, my camel book has a slightly different text ("such as a shell would do"), and if I look at your results, then this
      $ echo *jd *jd
      contradicts the behaviour in @t3 in my example.

      Bye
       PetaMem

        It's a BSD glob. The original BSD shell is the csh, not the Bourne shell or some derivate of that.
        $ ls *1 *1 not found $ csh % ls *1 No match. % exit $
        Abigail
        I also noticed that it contradicts to @t3 example, but command next to it explains glob('1') behaviour.

        And yes, I agree that glob's behaviour is somewhat tricky, or even more to say, weird.

        Courage, the Cowardly Dog.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://187019]
Approved by kodo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-25 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found