Search code examples
perldirectorymkdir

I use mkdir in Perl. It makes dir, but sometimes it adds a ? to the end of the directory name


Here is the code:

foreach my $name ( @unique_gene_list ) {
    print "$name\n";
    chomp $name; 

    unless(mkdir $name, 0700) {
        die "Unable to create directory called $name\n";
    }
}     

This works, mostly. For some reason, one of my directories is given a name that ends with a ?. I should note that the directory name showing in the terminal window shows the question mark. In the finder, there is no question mark at the end of that directory's name. I use Perl v.5.12.3, on a MAc OS 10.7.5.


Solution

  • The file from which you are reading has lines ending in CRLF. You are removing the LF with chomp, but the CR (represented by ^M) isn't removed. Instead of chomp;, use s/\s+\z//;.

    while (<$fh>) {
       s/\s+\z//;
       ...
    }