Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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.?"))' []

Comment on a glob w/ no wildcards returns itself
Select or Download Code
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 foo.pl --help shouldn't become perl foo.pl perl foo.pl infile outfile shouldn't become perl foo.pl 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2014-12-23 00:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (133 votes), past polls