0

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.

3
  • does Output exist? mkdir won't recursively create a structure for you. Commented Feb 22, 2016 at 20:39
  • If $file is directory, why is it called file? If $file is a dir then your test should be -d not -e. To recursively create dirs use File::Path. Commented Feb 22, 2016 at 20:40
  • Thank you for your help - Output does exist, though, and it's called file because it's not yet a directory. It made sense to me, but I'll admit I could have clearer variable names. Commented Feb 22, 2016 at 20:58

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.