user3837315
user3837315

Reputation: 1

How do I reference a file path containing spaces in Perl?

All I want to do is read in a list of directories from a file and list the files in the directories. The problem is that the directory paths have spaces in them, so this doesn't work:

#!/usr/bin/perl
use File::Spec;

open DIRNAMES, "<./dirnames";
while(my $dirname=<DIRNAMES>) {
    opendir DIR,$dirname or die $!;
    while (my $file = readdir(DIR)) {
    # Use a regular expression to ignore files beginning with a period
        next if ($file =~ m/^\./);
        print "$file\n";
    }
    closedir(DIR);
}
exit 0;

Nothing seems to work in line 6; $dirname doesn't work, neother does "$dirname", nor $"dirname"

Upvotes: 0

Views: 244

Answers (1)

ikegami
ikegami

Reputation: 386696

You forgot to remove the newline ending at the end of the line you placed in $dirname. Just add

chomp($dirname);

Upvotes: 1

Related Questions