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

I have the following:

@duplicate = glob "files/$department/*.txt"; foreach $item (@duplicate) { if ($item eq "files/$department/$number.txt") { $go_sub = 'yes'; } }

I have some directories that contain spaces and when a directory that does contain spaces the array doesn't match $item properly.

I think I've tried most if not all of the combinations of single/double quotes that makes sense to me....

Ideas, suggestions?

Thanks!

Replies are listed 'Best First'.
Re: File::DosGlob
by clemburg (Curate) on Jan 05, 2001 at 14:29 UTC

    From perldoc 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.

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com

Re: File::DosGlob
by eg (Friar) on Jan 04, 2001 at 22:46 UTC

    Well, I don't know about File::DosGlob in particular, but I know that the builtin glob doesn't work with spaces in the filenames (see perlop). Perhaps it best to just readdir and grep yourself.

    opendir DIR, $directory; @duplicate = map { "$directory/$_" } grep { /\.txt$/ } readdir DIR; closedir DIR;

      glob seems to work just fine for filenames with spaces on Win2K, at least. In a directory with two long filenames, this one-liner prints them both:

      perl -e "print map(qq($_\n),glob('*.txt'))"

        No, I think the problem is spaces in the wildcards, not in the matches that are returned.

                - tye (but my friends call me "Tye")
Re: File::DosGlob
by Hot Pastrami (Monk) on Jan 04, 2001 at 22:41 UTC
    Or, you could do it the old-fashioned way...
    opendir(DATADIR, "files/$department") or die "Invalid directory!"; @duplicate = readdir(DATADIR); closedir(DATADIR); foreach $item (@duplicate) { if ($item eq "files/$department/$number.txt") { $go_sub = 'yes'; last; } }
    I have usually used glob() myself, but I read recently that it's not the best way to do it, that it has some quirks on different platforms. As for what those quirks are, I haven't a clue.

    Update: However, in reading your code, it looks as though you may be simply trying to verify the presence of a file in that directory... perhaps you just want the -e operator, to see if the file exists:
    $go_sub = "yes" if -e "files/$department/$number.txt";


    Hot Pastrami
(tye) Spaces in wildcards (Re: File::DosGlob)
by tye (Sage) on Jan 05, 2001 at 01:16 UTC

    If you use File::Glob, then spaces in file wildcards are just fine.

    If you use File::DosGlob and then read the documentation, then you will find you can quote the spaces in your wildcard with " or \:

    use File::DosGlob; my @matches= glob( qq<"$file"> ); ( my $slashed= $file ) =~ s/\s/\\\1/g; my @files= glob( $slashed );
    These also work for me without use File::DosGlob (at least when running under Win32 -- I haven't tested this elsewhere).

            - tye (but my friends call me "Tye")
Re: File::DosGlob
by Albannach (Monsignor) on Jan 04, 2001 at 22:30 UTC
    Try something like this: @dups = glob qq~files/"$dir"/*.txt~;

    --
    I'd like to be able to assign to an luser