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

use strict; use warnings; use 5.010; say for (glob "*"); --output:-- 1perl.pl 2perl.pl 2perl.pl.bak 3perl.pl 3perl.pl.bak 4perl.pl data.txt data1.txt data2.txt data2.txt.out data3.txt examples helloWorld.pl in.txt out.txt Person.pm xml.xml

I've searched around, and everything I've read says glob returns full paths, and readdir returns just file names.

Replies are listed 'Best First'.
Re: glob: where are the full paths?
by ikegami (Patriarch) on Nov 14, 2009 at 07:09 UTC
    It gives *qualified* paths — not necessarily fully qualified paths — assuming you're getting it to returns paths at all. For example, glob '{a,b}{c,d}' doesn't returns paths at all.

      For example, glob '{a,b}{c,d}' doesn't returns paths at all.

      use strict; use warnings; use 5.010; say glob '{a,b}{c,d}'; --output:-- acadbcbd

      Uhm, where's the documentation for that?

      Ok, here's part of it:

      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. In scalar context, glob iterates through such filename expansions, returning undef when the list is exhausted.
      What do the curly braces do?
        Its in the manual , See File::Glob for details.
        The metanotation a{b,c,d}e is a shorthand for abe ace ade . Left to right order is preserved, with results of matches being sorted separately at a low level to preserve this order. As a special case {, }, and {} are passed undisturbed.
        $ perl -le"print for glob shift" "abe ace ade" abe ace ade $ perl -le"print for glob shift" "see also sea shore " see also sea shore $
Re: glob: where are the full paths?
by Anonymous Monk on Nov 14, 2009 at 05:36 UTC
    glob expands * but you have to give it full paths
    $ pwd C:\strawberry\c $ perl -le"print for glob shift" * bin contrib COPYING COPYING.LIB doc include lib libexec mingw32 share $ perl -le"print for glob shift" C:/strawberry/c/* C:/strawberry/c/bin C:/strawberry/c/contrib C:/strawberry/c/COPYING C:/strawberry/c/COPYING.LIB C:/strawberry/c/doc C:/strawberry/c/include C:/strawberry/c/lib C:/strawberry/c/libexec C:/strawberry/c/mingw32 C:/strawberry/c/share $ perl -le"print for glob shift" C:/strawberry/c/*/info/* C:/strawberry/c/share/info/dir C:/strawberry/c/share/info/make.info C:/strawberry/c/share/info/make.info-1 C:/strawberry/c/share/info/make.info-2 $
    Be careful to use forward slashes. For full caveats see 'perldoc -f glob' and File::Glob.

      Hi,

      Thanks for the response.

      glob expands * but you have to give it full paths

      Ok.

      Be careful to use forward slashes.

      Yep.

      For full caveats see 'perldoc -f glob' and File::Glob

      I read both before I posted. They weren't helpful. There doesn't seem to be anything in the docs that discusses the return value of glob. The only thing of note (to me) in the File::Glob docs was this:

      The C glob code has the following copyright: Copyright (c) 1989, 1993 The Regents of the University of Californ +ia. All rights reserved. This code is derived from software contributed to Berkeley by Guido van Rossum.
      .

      Monks, bow down and hail the BDFL. The True Path has been revealed.

        Another way to do it:

        #!/sw/bin/perl use strict; use warnings; use File::Glob ':glob'; my @files = </sw/bin/*>; foreach my $file (@files) { print $file . "\n"; }

        I'm using Tiger. Here, it shows me all the files in /sw/bin.

        Um, this is not slashdot.
Re: glob: where are the full paths?
by Marshall (Canon) on Nov 14, 2009 at 08:47 UTC
    This globbing stuff although it appears simple to use, can get messy, complex and is non-portable between platforms.

    I would open a directory and use readdir() with a Perl grep{} filter to get what you want. That will always work. It is code that is: easy to understand and portable between platforms and Perl versions. Example code..

    #!/usr/bin/perl -w use strict; my $dir = "."; opendir (DIR, $dir) || die "cannot open $dir\n"; my @filepaths = map{"$dir/$_"} grep{-f "$dir/$_"} readdir(DIR); print join("\n",@filepaths),"\n";