terdon terdon - 1 year ago 78
Perl Question

Why does Perl's glob() function always return a file name when given a string with no globbing characters?

I gave a list of globs and one string to Perl's

function. The globs were treated as expected but the string is always found. For example:

$ ls
$ perl -le '@files=glob("*bar"); print @files' ## prints nothing, as expected
$ perl -le '@files=glob("bar"); print @files'

As you can see above, the second example prints
even though no such file exists.

My first thought is that it behaves like the shell in that when no expansion is available, a glob (or something being treated as a glob) expands to itself. For example, in
(awful as it is, this is what Perl's
function seems to be following, see the quote below):

% foreach n (*bar*)
foreach: No match.

% foreach n (bar)
foreach? echo $n
foreach? end
bar ## prints the string

However, according to the docs,
should return filename expansions (emphasis mine):

In list context, returns a (possibly empty) list of filename expansions on the value of EXPR such as the standard Unix shell /bin/csh would do.

So why is it returning itself when there are no globbing characters in the argument passed to
? Is this a bug or am I doing something wrong?

Answer Source

I guess I expected Perl to be checking for file existence in the background.

Perl is checking for file existence:

$ strace perl -e'glob "foo"' 2>&1 | grep foo
execve("/home/mcarey/perl5/perlbrew/perls/5.24.0-debug/bin/perl", ["perl", "-eglob \"foo\""], [/* 39 vars */]) = 0
lstat("foo", {st_mode=S_IFREG|0664, st_size=0, ...}) = 0

So why is it returning itself when there are no globbing characters in the argument passed to glob?

Because that's what csh does. Perl's implementation of glob is based on glob(3) with the GLOB_NOMAGIC flag enabled:


Is the same as GLOB_NOCHECK but it only appends the pattern if it does not contain any of the special characters *, ? or [. GLOB_NOMAGIC is provided to simplify implementing the historic csh(1) globbing behavior and should probably not be used anywhere else.


If pattern does not match any pathname, then glob() returns a list consisting of only pattern...


  • if a matching file exists, the filename expansion is returned
  • if no matching file exists and the pattern contains no wild cards, the pattern is returned

In the case of a pattern with no wildcards, the filename expansion is the same as the pattern, so

glob 'foo'

in list context will always return a list with the single element foo, whether the file foo exists or not.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download