glob in perl

Discussion in 'Perl Misc' started by Andreas Boehm, Feb 9, 2004.

  1. Hello,

    I found the following behavior of perls built-in glob, but do not know
    if this is correct.
    in debug and in normal mode globbing does the folling thing:

    DB<1> @a=glob("/home/sunny/share/User/Joerg/rho- SAX tryp/*");
    DB<3> x @a
    0 '/home/sunny/share/User/Joerg/rho-'
    1 'SAX'
    DB<4>

    Is this correct?

    And does there exist a solution that correctly globs folders containing
    spaces in their names?

    viele Gruesse
    Andreas
     
    Andreas Boehm, Feb 9, 2004
    #1
    1. Advertising

  2. Andreas Boehm

    Ben Morrow Guest

    Andreas Boehm <> wrote:
    > I found the following behavior of perls built-in glob, but do not know
    > if this is correct.
    > in debug and in normal mode globbing does the folling thing:
    >
    > DB<1> @a=glob("/home/sunny/share/User/Joerg/rho- SAX tryp/*");
    > DB<3> x @a
    > 0 '/home/sunny/share/User/Joerg/rho-'
    > 1 'SAX'
    > DB<4>
    >
    > Is this correct?


    Yup.

    > And does there exist a solution that correctly globs folders containing
    > spaces in their names?


    Don't use folder(spit!)^W^W directories with spaces in their names. It
    will cause you much pain.

    File::Glob::bsd_glob will treat each argument as a separate pattern,
    rather than splitting on whitespace.

    Ben

    --
    It will be seen that the Erwhonians are a meek and long-suffering people,
    easily led by the nose, and quick to offer up common sense at the shrine of
    logic, when a philosopher convinces them that their institutions are not based
    on the strictest morality. [Samuel Butler, paraphrased]
     
    Ben Morrow, Feb 9, 2004
    #2
    1. Advertising

  3. Andreas Boehm <> wrote:

    > I found the following behavior of perls built-in glob,



    > And does there exist a solution that correctly globs folders containing
    > spaces in their names?



    You can do it without using glob at all:

    # untested
    my $dir = '/home/sunny/share/User/Joerg/rho- SAX tryp';
    opendir DIR, $dir or die "could not open '$dir' directory $!";
    my @a = sort map { "$dir/$_" } grep /^[^.]/, readdir DIR;
    closedir DIR;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Feb 9, 2004
    #3
  4. Andreas Boehm <> wrote:
    > And does there exist a solution that correctly globs folders containing
    > spaces in their names?


    Try: glob '/path/to/dir\ with\ spaces/*';
    Note the single quotes to protect the backslashes.

    or:
    $dir = '/path/to/dir with spaces';

    --
    Glenn Jackman
    NCF Sysadmin
     
    Glenn Jackman, Feb 9, 2004
    #4
  5. hi,

    >> DB<1> @a=glob("/home/sunny/share/User/Joerg/rho- SAX tryp/*");
    >> DB<3> x @a
    >>0 '/home/sunny/share/User/Joerg/rho-'
    >>1 'SAX'
    >> DB<4>
    >>Is this correct?


    why?

    >>And does there exist a solution that correctly globs folders containing
    >>spaces in their names?

    > Don't use folder(spit!)^W^W directories with spaces in their names. It
    > will cause you much pain.


    but unix allows spaces in names since the seventies of the last century.

    > File::Glob::bsd_glob will treat each argument as a separate pattern,
    > rather than splitting on whitespace.


    but the argument is quoted...

    regards,
    Andreas
     
    Andreas Boehm, Feb 9, 2004
    #5
  6. Andreas Boehm

    Ben Morrow Guest

    Andreas Boehm <> wrote:
    > >> DB<1> @a=glob("/home/sunny/share/User/Joerg/rho- SAX tryp/*");
    > >> DB<3> x @a
    > >>0 '/home/sunny/share/User/Joerg/rho-'
    > >>1 'SAX'
    > >> DB<4>
    > >>Is this correct?

    > >
    > > Yup

    >
    > why?


    Because that's what glob does! RTFM.

    If you want the reason behind glob's behaviour, it is because 1. glob
    used to fork csh to do the globbing and that's what csh did and
    2. people with sense don't use names with spaces in so it's useful to
    be able to do <*.c *.h> and have it DWIM.

    > >>And does there exist a solution that correctly globs folders containing
    > >>spaces in their names?

    >
    > > Don't use folder(spit!)^W^W directories with spaces in their names. It
    > > will cause you much pain.

    >
    > but unix allows spaces in names since the seventies of the last
    > century.


    Unix allows any character in a filename except "\0" and /. That
    doesn't mean using them is a good idea. Having spaces in names is fine
    as long as all your tools are prepared to deal with that fact, and a
    lot of unix tools are not. (glob and system/exec with one arg are the
    only places I can think of where Perl does an implicit split on
    whitespace, so it's a good tool to use to replace those like shell
    that do it everywhere.)

    > > File::Glob::bsd_glob will treat each argument as a separate
    > > pattern, rather than splitting on whitespace.

    >
    > but the argument is quoted...


    Perl Is Not Shell. glob takes one argument, splits that on whitespace,
    and treats each part as a separate glob pattern. RTFM.
    File::Glob::bsd_glob takes several arguments, and doesn't split them.

    Ben

    --
    For the last month, a large number of PSNs in the Arpa[Inter-]net have been
    reporting symptoms of congestion ... These reports have been accompanied by an
    increasing number of user complaints ... As of June,... the Arpanet contained
    47 nodes and 63 links. [ftp://rtfm.mit.edu/pub/arpaprob.txt] *
     
    Ben Morrow, Feb 9, 2004
    #6
  7. Andreas Boehm

    Peter Scott Guest

    In article <c083uj$13o9jq$>,
    Andreas Boehm <> writes:
    >Hello,
    >
    >I found the following behavior of perls built-in glob, but do not know
    >if this is correct.
    >in debug and in normal mode globbing does the folling thing:
    >
    > DB<1> @a=glob("/home/sunny/share/User/Joerg/rho- SAX tryp/*");
    > DB<3> x @a
    >0 '/home/sunny/share/User/Joerg/rho-'
    >1 'SAX'
    > DB<4>
    >
    >Is this correct?


    Yes. (glob() since 5.6.0 uses File::Glob, which uses BSD glob, which does not
    consider ENOENT to be an error.)

    >And does there exist a solution that correctly globs folders containing
    >spaces in their names?


    A couple come to mind:

    $dir = "/home/sunny/share/User/Joerg/rho- SAX tryp";
    @a = glob("\Q$dir\E/*");

    @a = glob("'/home/sunny/share/User/Joerg/rho- SAX tryp/*'");

    If you think about it, you would not get away with leaving those spaces
    unprotected on a command line operation, so why should Perl's glob
    behave any differently from the shell's?

    --
    Peter Scott
    http://www.perldebugged.com/
    *** NEW *** http//www.perlmedic.com/
     
    Peter Scott, Feb 9, 2004
    #7
  8. Ben Morrow <> writes:

    > Andreas Boehm <> wrote:


    >> And does there exist a solution that correctly globs folders containing
    >> spaces in their names?

    >
    > Don't use folder(spit!)^W^W directories with spaces in their names. It
    > will cause you much pain.


    That is not an acceptable constraint. Perl is often used for system
    maintenance utilities where I have to deal with whatever directory
    structure users have created. I don't control that namespace, and so
    far as I know none of the Unix variants give me any mechanism to
    enforce a "no spaces" rule.

    > File::Glob::bsd_glob will treat each argument as a separate pattern,
    > rather than splitting on whitespace.


    Ah. That, on the other hand, may be a useful approach.
    --
    David Dyer-Bennet, <mailto:>, <http://www.dd-b.net/dd-b/>
    RKBA: <http://noguns-nomoney.com> <http://www.dd-b.net/carry/>
    Photos: <dd-b.lighthunters.net> Snapshots: <www.dd-b.net/dd-b/SnapshotAlbum/>
    Dragaera/Steven Brust: <http://dragaera.info/>
     
    David Dyer-Bennet, Feb 9, 2004
    #8
  9. Ben Morrow <> writes:

    > Andreas Boehm <> wrote:
    >> >> DB<1> @a=glob("/home/sunny/share/User/Joerg/rho- SAX tryp/*");
    >> >> DB<3> x @a
    >> >>0 '/home/sunny/share/User/Joerg/rho-'
    >> >>1 'SAX'
    >> >> DB<4>
    >> >>Is this correct?
    >> >
    >> > Yup

    >>
    >> why?

    >
    > Because that's what glob does! RTFM.


    I just went back and R the FM again, including going down into perlop
    from the mention in perlfunc, and it *still* doesn't document this
    behavior or show any examples of it.

    This probably explains many mysterious problems I've had over the
    years. Always good to learn something new!
    --
    David Dyer-Bennet, <mailto:>, <http://www.dd-b.net/dd-b/>
    RKBA: <http://noguns-nomoney.com> <http://www.dd-b.net/carry/>
    Photos: <dd-b.lighthunters.net> Snapshots: <www.dd-b.net/dd-b/SnapshotAlbum/>
    Dragaera/Steven Brust: <http://dragaera.info/>
     
    David Dyer-Bennet, Feb 9, 2004
    #9
  10. Andreas Boehm

    Ben Morrow Guest

    David Dyer-Bennet <> wrote:
    > Ben Morrow <> writes:
    > >
    > > Because that's what glob does! RTFM.

    >
    > I just went back and R the FM again, including going down into perlop
    > from the mention in perlfunc, and it *still* doesn't document this
    > behavior or show any examples of it.


    True... the nearest perlfunc comes to it is 'as /bin/csh would do',
    which I guess implies split-on-whitespace, but most of us are
    (thankfully) not familiar with csh!

    However, at least the 5.8 perlfunc also points to File::Glob, which
    explicitly explains it in the second para.

    Ben

    --
    Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
    From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
    (Ted Hughes, [ Heracles shoots Vulture with arrow. Vulture bursts into ]
    /Alcestis/) [ flame, and falls out of sight. ]
     
    Ben Morrow, Feb 9, 2004
    #10
  11. Ben Morrow <> writes:

    > David Dyer-Bennet <> wrote:
    >> Ben Morrow <> writes:
    >> >
    >> > Because that's what glob does! RTFM.

    >>
    >> I just went back and R the FM again, including going down into perlop
    >> from the mention in perlfunc, and it *still* doesn't document this
    >> behavior or show any examples of it.

    >
    > True... the nearest perlfunc comes to it is 'as /bin/csh would do',
    > which I guess implies split-on-whitespace, but most of us are
    > (thankfully) not familiar with csh!
    >
    > However, at least the 5.8 perlfunc also points to File::Glob, which
    > explicitly explains it in the second para.


    There is a reference to that in 5.6.1 as well.
    --
    David Dyer-Bennet, <mailto:>, <http://www.dd-b.net/dd-b/>
    RKBA: <http://noguns-nomoney.com> <http://www.dd-b.net/carry/>
    Photos: <dd-b.lighthunters.net> Snapshots: <www.dd-b.net/dd-b/SnapshotAlbum/>
    Dragaera/Steven Brust: <http://dragaera.info/>
     
    David Dyer-Bennet, Feb 10, 2004
    #11
  12. Andreas Boehm

    Bart Lateur Guest

    Andreas Boehm wrote:

    >And does there exist a solution that correctly globs folders containing
    >spaces in their names?


    On Windows, this works:

    #!perl -l
    print for glob('"c:/My Documents/*.txt"');

    Note the extra double quotes around the path, inside the string.

    --
    Bart.
     
    Bart Lateur, Feb 10, 2004
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Georgy Pruss
    Replies:
    15
    Views:
    758
    Tim Roberts
    Dec 1, 2003
  2. Tim Peters
    Replies:
    1
    Views:
    384
    Duncan Booth
    Dec 1, 2003
  3. Sean Berry

    Question about glob.glob <--newbie

    Sean Berry, May 4, 2004, in forum: Python
    Replies:
    3
    Views:
    359
    David M. Cooke
    May 4, 2004
  4. Elbert Lev

    glob.glob unicode bug or feature

    Elbert Lev, Jul 31, 2004, in forum: Python
    Replies:
    5
    Views:
    420
    Neil Hodgson
    Aug 2, 2004
  5. Hitesh

    glob.glob output

    Hitesh, Mar 12, 2007, in forum: Python
    Replies:
    6
    Views:
    419
    Hitesh
    Mar 13, 2007
Loading...

Share This Page