somilsharma
somilsharma

Reputation: 79

Unable to create directory using make_path

I was trying to create a directory and a sub-directory within it using Perl. Here is the code:

  #!/usr/bin/perl
  use strict;
  use warnings;
  use Cwd qw(abs_path);
  use File::Path qw(make_path remove_tree);
  my $path = abs_path();
  my @create = make_path($path , '/test/data' , {
                                                  verbose => 1,
                                                  mode => 0777,
                                                })
               or die "failed to create directory /test/data $!";

It shows the following error:-

    failed to create directory /test/data  at ./perl_project.pl line 7

What I am doing wrong?

Upvotes: 1

Views: 303

Answers (1)

toolic
toolic

Reputation: 62154

If you want to create the path /test/data at your current directory, change:

 my @create = make_path($path , '/test/data' , {

to:

 my @create = make_path($path . '/test/data' , {
 #                            ^

Since you used a comma in make_path, your code attempts to create 2 directories: one at the current directory ($path) and another at the absolute path /test/data.

Upvotes: 2

Related Questions