glob in perl

A

Andreas Boehm

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
 
B

Ben Morrow

Andreas Boehm said:
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
 
T

Tad McClellan

Andreas Boehm said:
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;
 
G

Glenn Jackman

Andreas Boehm said:
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';
 
A

Andreas Boehm

hi,
why?

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
 
B

Ben Morrow

Andreas Boehm said:

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 said:
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.)
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
 
P

Peter Scott

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?
 
D

David Dyer-Bennet

Ben Morrow said:
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.
 
D

David Dyer-Bennet

Ben Morrow said:
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!
 
B

Ben Morrow

David Dyer-Bennet said:
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
 
D

David Dyer-Bennet

Ben Morrow said:
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.
 
B

Bart Lateur

Andreas said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top