Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Portability of glob function in a modern perl

by Corion (Patriarch)
on Aug 12, 2005 at 07:59 UTC ( [id://483203]=note: print w/replies, xml ) Need Help??


in reply to Portability of glob function in a modern perl

It became "portable" in the sense that it behaves in its broken way everywhere, regardless of what shell is installed now, because the functionality was moved into Perl.

It is quite common for Perl scripts using glob to break on Win32 because the author could not imagine that whitespace is a thing that happens in filenames and directories. So for anything but trivial hacks, File::Find or File::Find::Rule are to be preferred over glob.

  • Comment on Re: Portability of glob function in a modern perl

Replies are listed 'Best First'.
Re^2: Portability of glob function in a modern perl
by itub (Priest) on Aug 12, 2005 at 16:15 UTC
    I don't see the problem with glob and whitespace in filenames. As long as the user understands that glob uses csh rules and escapes whitespace appropriately, it works the same in Windows and in Unix.

    Added: Thanks for the examples, now I see what you mean (I've made the same mistakes in the past!).

      A quick search through CPAN (provided by http://cpansearch.bulknews.net/) provides me with following ugly usages of glob that will not work for quoted spaces in the path, as the path is otherwise used for directly constructed filenames too:

      Mac::iPod::GNUPod

      ... my $path = File::Spec->catdir($self->{mnt}, @path); ... # and $self->add_song(glob(File::Spec->catpath($self->{mnt}, "iPod_Control +", "Music", "*", "*")));

      ($self->{mnt} is supposed to be the mount point of the iPod)

      Mail::Action::Role::Purge

      my $dir = $self->storage_dir; ... my @files = map { scalar( s/\.$extension//, basename $_) } glob("$dir/*.$extension");

      ($self->storage_dir is supposed to be the directory for storage, unquoted)

      CDDB::Fake

      ... foreach my $dir ( @ARGV ) { unless ( -d $dir ) { warn("$dir: not a directory -- skipped\n"); next; } ... my @files = sort map { $1 if /([^\/]+)\.mp3$/ } glob("$dir/[0-9]*.mp +3");

      Same as above

      I stopped here, on page three of the results (when searching for glob).In short, a directory and it's glob-safe quoted string are not usable interchangeably, and thus will lead to problems with the authors that can't imagine/foresee whitespace in filenames.

      As an aside, the cpansearch doesn't seem to like searching for "glob(", which would exclude a lot of "typeglob" results.

Re^2: Portability of glob function in a modern perl
by bsb (Priest) on Jul 18, 2008 at 06:30 UTC
    Does using File::Glob::bsd_glob() instead solve the spaces issue? Or are there other traps?

      No - for the cases where I use glob, I always have use File::Glob qw(bsd_glob); at the top of my script. When using glob, bsd_glob is the sane and safe way.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://483203]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2024-04-18 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found