thadc has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I have a need to create a directory on file system from within my perl script, and I have to assume the directory path is not fixed. I can successfully execute something like:

`mkdir folderA/folderB/folderC`

, but the directory path is variable, so I have to somehow pass the path variable as part of the shell command. However, I do not know how to do this. Any ideas are appreciated. Thank you!

Replies are listed 'Best First'.
Re: pass a variable or string to shell command from perl script (updated)
by haukex (Archbishop) on Dec 27, 2017 at 19:05 UTC

    First of all, marto is right that you don't need to call an external command for this, there is mkdir, and also make_path from the core module File::Path (Update: which can do the equivalent of mkdir -p). For proper handling of filenames, see the core module File::Spec, or, with a nicer interface, Path::Class from CPAN.

    use File::Spec::Functions qw/catdir/; use File::Path qw/make_path/; make_path(catdir('folderA','folderB','folderC')); # Update 2: OR: use Path::Class qw/dir/; dir('folderA','folderB','folderC')->mkpath;

    Only for the sake of completeness: I wrote about different ways to call external commmands (safely) at length here.

      Thank you. Perl mkdir function definitely the way to go. Working like a charm!

Re: pass a variable or string to shell command from perl script
by marto (Cardinal) on Dec 27, 2017 at 18:39 UTC

    Is there a reason not to use mkdir?

Re: pass a variable or string to shell command from perl script
by Laurent_R (Canon) on Dec 27, 2017 at 21:33 UTC
    Just as my fellow monks before, I would really recommend to use the Perl mkdir internal command.

    However, just to complete the answer to your question, you can very well pass a Perl variable to a backtick command line (or qx// or system command).

    By the way, here, system would probably be better than backticks (or qx//) because you don't want to retrieve any output from the system command you're issuing, but you're probably interested (and should be) to know if the command succeeded or returned an error.

    Having said that, and repeating once again, I would very much recommend to use the Perl mkdir internal command (or some of the modules recommended earlier by haukex).

      system would probably be better than backticks (or qx//) because you don't want to retrieve any output from the system command you're issuing, but you're probably interested (and should be) to know if the command succeeded or returned an error.

      See also The problem of "the" default shell for why you want the multi-argument form of system if you can't use a perl build-in function or a module.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: pass a variable or string to shell command from perl script
by karlgoethebier (Abbot) on Dec 28, 2017 at 18:24 UTC
    "...the shell command..."

    TMTOWTDI without shell out:

    karls-mac-mini:playground karl$ mkdir 1206287 karls-mac-mini:playground karl$ cd 1206287/ perl -MPath::Tiny -E 'path(q(folderA/folderB/folderC))->mkpath;' karls-mac-mini:1206287 karl$ find . -type d . ./folderA ./folderA/folderB ./folderA/folderB/folderC

    If this is what you want.

    See also Path::Tiny.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help