2

I am using a user input variable as my initial directory name, but when trying to expand the directory and create sub-folders, utilizing that variable causes issue in the path.

#!/usr/bin/perl
use strict;
use warnings;

print "What would you like to name your directory?\n"; 
chomp( my $directory = <STDIN> );

mkdir $directory, 0755;
mkdir $directory/data, 0755; ##<----<<Error begins here##
mkdir $directory/data/image, 0755;
mkdir $directory/data/cache, 0755;
3
  • 4
    use diagnostics; and try again. Commented Sep 26, 2014 at 13:36
  • That helped out immensely, awesome. Commented Sep 26, 2014 at 13:39
  • Kudos for use strict; and use warnings;. Tiz nice to see a new user having already learned that most important lesson. Commented Sep 26, 2014 at 20:44

2 Answers 2

4

Unquoted / in Perl means division or regular expression match. Quote it if it's part of a string:

mkdir "$directory/data", 0755;
Sign up to request clarification or add additional context in comments.

Comments

3

Or try

use File::Path qw/ make_path /;

make_path( "${directory}/data/image", "${directory}/data/cache" );

it will create the intermediate directories for you.

You may also want to look at the Path::Tiny or Path::Class modules, which have a nicer OO-interface for file operations.

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.