Lisa
Lisa

Reputation: 959

Mkdir perl error: no such file

I am trying to make a directory in the following code:

# reads in files from a corpus
    for my $corpusFile (glob("./Corpus/*.txt")) {

    # makes part of the name of the file the name for a directory
    my $file = substr ($corpusFile, 9, 6);
    my $outputFiles = "./Output/$file";
    mkdir $outputFiles unless -e $outputFiles or die "Cannot make file directory: $!";

However, I get the error: Cannot make file directory: No such file or directory at perl/corpus.pl

I don't really understand this error. Of course the directory doesn't exist - I'm trying to create it.

To explain my file structure a bit: I have a big folder that contains an "output" folder and a "perl" folder - my perl code is in the folder called "perl" and I'm trying to create a directory in "output." I am very new to perl, so I apologize if the answer here is obvious.

Upvotes: 0

Views: 849

Answers (1)

Hunter McMillen
Hunter McMillen

Reputation: 61512

If you look at how this expression:

mkdir $outputFiles 
    unless -e $outputFiles 
or die "Cannot make file directory: $!";

is parsed by Perl, the answer is apparent. You can see how this is parsed using (-p prints parentheses):

perl -MO=Deparse,-p \ 
     -e 'mkdir "foo" unless -e "foo" or die "Cannot create foo: $!"

produces:

(((-e 'foo') or die("Cannot create foo: $!")) or mkdir('foo'));
-e syntax OK

Now you should be able to see the issue, according to the above expression:

1) If directory 'foo' does not exist, the program will die with the message 'Cannot create foo: No such file or directory'

2) If directory 'foo' does exist, the program will call mkdir 'foo'. This is pointless however, since it already exists.

Upvotes: 4

Related Questions