1
checkAndCreateDirectory();
moveFiles();
sub checkAndCreateDirectory {
    my $dirname = "launchpad/config/com/adobe/granite/auth/saml/SamlAuthenticationHandler";
    my @folders = split /\/|\\/, $dirname;
    map { mkdir $_; chdir $_; } @folders;
}
sub moveFiles {
    my $source_dir = "SamlAuthenticationHandler";
    my $destination_dir = "launchpad/config/com/adobe/granite/auth/saml/SamlAuthenticationHandler";
    opendir(my $DIR, $destination_dir) || die "can't opendir $source_dir: $!";
    move("$source_dir", "$destination_dir") or die "FAIL : Unable to add config -> $!";
}

checkAndCreateDirectory() and moveFiles() subroutines are running fine with sepaerate script but when tried to run in same script it throws error : No such file or directory

Can anyone please help me in figuring out the issue? Is there any issue with

map { mkdir $; chdir $; } @folders;

?

4
  • 3
    Try print the error message in $!: mkdir $_ or die "Could not create folder '$_': $!" Commented Apr 7, 2017 at 10:25
  • 4
    Se also make_path in File::Path it will create all the nonexistent directories in one call Commented Apr 7, 2017 at 10:32
  • 6
    Using map in a void context is a sign you probably should have been using for instead. Commented Apr 7, 2017 at 10:41
  • @HåkonHægland I tried printing the error msg but all I am getting is "No such file or directory at run.pl line 11". And thanks for your help, make_path is working fine and is creating nonexistent directories as well but I am just curious to find out the problem with above mentioned code. Commented Apr 9, 2017 at 4:21

1 Answer 1

3

The issue is that you chdir into all of your subdirectories of checkAndCreateDirectory but never return to where you started, so when you call moveFiles, you are in the wrong place and cannot find your $destination_dir.

I would simplify your script like this:

use warnings;
use strict;

use File::Copy::Recursive qw( dirmove );
use File::Path qw( make_path );

my $saml_handler_path = 'launchpad/config/com/adobe/granite/auth/saml/SamlAuthenticationHandler';

make_path $saml_handler_path
    or die "Unable to create path '$saml_handler_path' : $!";

my $source_dir = "SamlAuthenticationHandler";
my $dest_dir = $saml_handler_path;

dirmove( $source_dir, $dest_dir )
    or die "Unable to move $source_dir to $dest_dir : $!";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.