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

Hi,

I'm trying to write a piece of code that will replace the contents of @ARGV with actual valid files which exist in a certain directory. I need to allow for wildcards ('*' only), spaces in file names and when a user specifies a directory I need to include the contents of that directory.

This is what I have so far:
use File::Find::Rule; my $root_in = "E:/PGPTool/dept/AIS/dropbox"; aprint "Before:\n"; foreach (@ARGV) { print "$_\n"; } # replace wildcards and directories in @ARGV with actual files/folders @ARGV = map glob ("$root_in/$_"), @ARGV; eval { @ARGV = find (file => in => \@ARGV) }; foreach (@ARGV) { $_ =~ s/($root_in\/)(.+)/$2/; } print "\nAfter:\n"; foreach (@ARGV) { print "$_\n"; }
The only problem is it won't work for files with spaces in the name - the '@ARGV = map glob ...' line uses the space as a delimiter and assumes eg 'file1 .txt' is refering to two files : 'file1' and '.txt'.

I tried inserting apostrophes :
glob ("'$root_in/$_'"), @ARGV;
which fixes the problem, but brakes it for files which do not contain spaces.

Anyone any ideas on how to fix this?

Thanks,
A

Replies are listed 'Best First'.
Re: Replacing specified files with actual files using map glob (modules)
by tye (Sage) on Apr 06, 2004 at 15:59 UTC

    "perldoc File::Glob" and "perldoc File::DosGlob" both document how to deal with spaces. From File::Glob's documentation:

    Since v5.6.0, Perl's CORE::glob() is implemented in terms of bsd_glob(). Note that they don't share the same prototype--CORE::glob() only accepts a single argument. Due to historical reasons, CORE::glob() will also split its argument on whitespace, treating it as multiple patterns, whereas bsd_glob() considers them as one pattern.

    so you could use File::Glob qw( bsd_glob ); and replace your calls to glob() with calls to bsd_glob().

    From the documentation for File::DosGlob:

    Spaces in the argument delimit distinct patterns, so `glob('*.exe *.dll')' globs all filenames that end in `.exe' or `.dll'. If you want to put in literal spaces in the glob pattern, you can escape them with either double quotes, or backslashes. e.g. `glob('c:/"Program Files"/*/*.dll')', or `glob('c:/Program\ Files/*/*.dll')'. The argument is tokenized using `Text::ParseWords::parse_line()', so see the Text::ParseWords manpage for details of the quoting rules used.

    - tye        

Re: Replacing specified files with actual files using map glob
by Anonymous Monk on Apr 06, 2004 at 17:05 UTC
    Think I've got this figured out : if I escape the spaces before doing the '@ARGV = map glob ...' line, the code picks them up OK.

    Thanks anyway,
    A