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

fellow monks,

I need some information about the perl's glob function.

We pass Clearcase MVFS directory path as argument to the glob. These MVFS directory paths are usually quite long. The return of the glob function for these paths will be 1 or more paths. But we have a case where nothing is returned by the glob function. On further analysis i noticed that the length of the path is 314 characters for the specific case and for the rest of the cases it is very much lesser (around 200 or lesser).

My doubt is whether there is any limitation like the length of the argument must be less than 256 chars. I checked the perl documentation and also in the internet. But i could not find any detailed information about the glob function.

  • Comment on perl's glob function - argument passing

Replies are listed 'Best First'.
Re: perl's glob function - argument passing
by jdporter (Paladin) on Jan 17, 2007 at 02:55 UTC

    If you read the glob documentation, you see that it is implemented (since perl 5.6) by File::Glob. There, you read that it's implemented "in terms of" File::Glob's bsd_glob routine. But apparently there's a functional difference between glob and bsd_glob: the former considers its argument to be one or more whitespace-sepearated patterns, whereas the latter considers it a single argument, even if it includes whitespace. So I wonder: does this long path you have contain any spaces? If so, try loading File::Glob and using its bsd_glob routine instead.

    Another avenue would be to try another module which provides an alternative glob, such as File::DosGlob and FastGlob. (All these modules let you override the core glob with their version, if you want.)

    A word spoken in Mind will reach its own level, in the objective world, by its own weight
      That's only part of the story.

      Even though I'm no C expert, I did some hunting around in the internals of File::Glob. bsd_glob calls doglob. In addition, the module uses XSLoader, so, it's an XS module. :) Looking at the current source of Glob.xs, doglob in turn calls the C function (not an XS function) bsdglob, which in turn can be found in bsd_glob.c. In turn, that calls the C function glob0, in the same source file. Are you still with me? Now the documentation for glob0 says:

      /* * The main glob() routine: compiles the pattern (optionally processin +g * quotes), calls glob1() to do the real pattern matching, and finally * sorts the list (unless unsorted operation is requested). Returns 0 * if things went well, nonzero if errors occurred. It is not an erro +r * to find no matches. */
      So, it appears to be the "main routine".

      Now, and this is the interesting part: it allocates buffers with a length MAXPATHLEN. This constant is defined in perl.h, and it's platform dependent. Anyway, usually this constant is an IMO rather small number, as the code comments there state:

      # define MAXPATHLEN 1024 /* Err on the large side. */
      Note the comment: 1024 is considered "large".

      Now, to answer the OP's question: yes, it looks like there's a built in limit on the length of the strings that glob will return, but the exact maximum value depends on your platform.

      And I'm not sure I did all of my homework well, I might still have overlooked something essential.

Re: perl's glob function - argument passing
by ferreira (Chaplain) on Jan 17, 2007 at 10:00 UTC

    On reading glob and File::Glob, there seems to be no hard limit on the length of the pattern. There is a possible limit if you call for it explicitly via bsd_glob($pat, GLOB_LIMIT) but this is about the glob expansions and not the argument.

    As pointed out in a previous reply, maybe you should avoid the specials of the glob function (like POSIX compliance and split on whitespace) by using bsd_glob directly. Also make some experiments by changing to a directory in Clearcase MVFS directory and expanding a shorter relative path. Maybe it is not working for other reasons, like permissions.

Re: perl's glob function - argument passing
by sgt (Deacon) on Jan 17, 2007 at 09:32 UTC

    if you have a shell, does it work the way you expect on this directory? does the pattern used with perl glob make a difference? and in the shell?

    if glob is not enough, you might pipe-in the output of the ls (or equivalent) command (or even get it all in one go with qx{...})

    open $globbing, "ls -1a |" or die; while (<$globbing>) { chomp filter; ... }
    hth --stephan