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

a glob w/ no wildcards returns itself

by jeberle (Initiate)
on Aug 23, 2011 at 17:53 UTC ( #921952=perlquestion: print w/replies, xml ) Need Help??
jeberle has asked for the wisdom of the Perl Monks concerning the following question:

When I supply a pattern w/o any glob characters, I get the pattern back, even if no file matches it. Why does Perl's glob do this?

$ echo foo.c foo.c $ echo foo.? foo.? $ ls foo.c ls: foo.c: No such file or directory $ ls foo.? ls: foo.?: No such file or directory $ perl -le 'print glob "foo.c"' foo.c # shell expansion w/o existence check $ perl -le 'print glob "foo.?"' # shell expansion w/ existence check

Glob's behavior w/o wildcards matches a simple shell expansion, while glob's behavior w/ wildcards matches a file existence test.

Just to make this sting a little, Python's behavior is consistent:

$ python -c 'from glob import glob; print(glob("foo.c"))' [] $ python -c 'from glob import glob; print(glob("foo.?"))' []

Replies are listed 'Best First'.
Re: a glob w/ no wildcards returns itself
by Eliya (Vicar) on Aug 23, 2011 at 19:08 UTC

    While you can certainly argue for (or against) any of the different globbing behaviors - ranging from "the shell"'s (interpret wildcards literally, if no match in the file system) to Python's (always test for existence) - ultimately, it's a matter of preference.

    Personally, I find Perl's mixed behavior rather "do-what-I-mean".  I.e., when I specify a ? or * wildcard, I'm typically interested in what matching files there are in the file system, while if I use a "limited set" wildcard like {a,b,c}, I can compactly compute permutations with it, e.g.

    $ perl -le 'print for glob "{foo,bar}{baz,quux}"' foobaz fooquux barbaz barquux

    which Python doesn't do because of its consistent behavior.  IOW, consistent isn't always the most practically useful... :)

    And, as for the shell, I always found its behavior to treat a "*" literally (in case nothing matches) a little counter-intuitive... (if I really mean a literal "*", I can always escape it in Perl).

    Anyhow, YMMV.

      Thank you Eliya. The use case I have is to do actual file matching, where the user specifies the pattern. For this, the behavior is counter-productive. Therefore:
      $ perl -le 'print grep { -f } glob "foo.c"'
      will have to rule the day.

      In bash you can do:

      $ shopt -s nullglob $ echo foo.?
      but then ls generates full dir listings on failed globs.
Re: a glob w/ no wildcards returns itself
by ikegami (Pope) on Aug 23, 2011 at 20:01 UTC

    It makes no sense to check if the file exists when wild cards aren't present.

    perl --help shouldn't become perl perl infile outfile shouldn't become perl infile

    It makes no sense to not honour wild cards. If you ask for all files matching «foo.?», it makes no sense to return «foo.?» unless «foo.?» exists. If you want the glob to return «foo.?», you'd pass «foo.\?».

    PS — Perl matches csh's behaviour. You tested using the wrong shell.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://921952]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2016-10-25 04:06 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (314 votes). Check out past polls.