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

I am trying to use perl to run two different commands - both of which need to be executed in a separate command prompt. Here are some of the details:

when I run

system('start cmd /k "cd c:\PerlExamples && perl Perl_Ex_1.pl"');

it works as expected - it opens a new command prompt, paths to the directory I need and runs my script.

However, I want to use this system... command in a loop where each time I am running a different example script. But when I run the following lines of code,

my $cmd = "perl Perl_Ex_1.pl"; system("start cmd /k cd c:\PerlExamples && $cmd");

it opens a new command prompt and paths to the directory I need. But it runs my example script in the original window and not in the newly opened command prompt.

Is there a way for me to do this? Thank you.

Replies are listed 'Best First'.
Re: How to execute multiple commands through perl script
by BrowserUk (Patriarch) on Oct 04, 2016 at 20:01 UTC

    Try it this way:

    my $cmd = "perl Perl_Ex_1.pl"; system qq[ start /D c:\PerlExamples cmd /k $cmd ];

    See start /? at your command prompt for the explanation of /D


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Thank you very much. This works too. Again I had to use \\PerlExamples so it wouldn't consider \P as an escape sequence. <\p>

Re: How to execute multiple commands through perl script
by pryrt (Abbot) on Oct 04, 2016 at 19:45 UTC

    Rhetorical Question: What's different between the two examples? My Answer: You lost your quote marks around the command being sent to cmd /k, so the && was being processed by the system-level cmd.exe rather than the embedded one.

    Here, I will use the qq{} double-quote notation to nest the quotes, so interpolation will still happen properly:

    my $cmd = "perl Perl_Ex_1.pl"; system(qq{start cmd /k "cd c:\PerlExamples && $cmd"});

    Thus, system runs the whole command (everything within the qq{}), and cmd /k runs everything in the normal quote characters (so both the cd and the $cmd).

    update: edited first paragraph for clarity

      Thank you very much. This works. I also had to change my path name to c:\\PerlExamples as it was trying to read \P as an escape sequence.

Re: How to execute multiple commands through perl script
by Ratazong (Monsignor) on Oct 04, 2016 at 19:44 UTC

    What happens when you execute

    my $cmd2 = "start cmd /k cd c:\PerlExamples && $cmd"; system ($cmd2);
    or putting it the other way around: get executing the combined command as you wish, and then you would also be able to run it in your loop. Btw.: printing the command (in my case $cmd2) shows you how perl does substitution inside paranthesis (" or ')

    Hope this helps you! Rata

      This again creates a new command prompt window and paths to my directory but executes the example script in the original window.

Re: How to execute multiple commands through perl script
by Anonymous Monk on Oct 05, 2016 at 00:14 UTC

    Continuing on this topic, is it possible to run the commands on separate lines in the newly opened command prompt?

    So my perl script needs to open a new command prompt and in that, connect to some server (I have a script that connects to the server) and wait for connection to be done before execution of next command. This is what I am currently doing:

    my $cmd1 = "python Pyscript.py server=server_name.com"; system(qq{start cmd /k "cd c:\\PerlExamples && $cmd1 && test=$test_nam +e,$file_name"});

    $test_name and $file_name are previously defined. Execution of this script paths to my directory and connects to my server but doesn't execute the test part. When I try to manually enter these commands in a command prompt window, it works if I have them on separate lines only. Is it possible to do this from perl?

      Please, show the separate lines you used. Was it
      cd c:\\PerlExamples python Pyscript.py server=server_name.com test=$test_name,$file_name

      The last line really seems weird. Or do you want to use the last two lines as one line only? Then drop the && after $cmd1 .

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

        I need to use the last line test=... on a separate line after the python Pyscript... line has executed. This is what I need to do

        cd c:\\PerlExamples && python Pyscript.py server=server_name.com #The above part can be in one or two lines - it doesn't matter test=$test_name,$file_name #This has to be on separate line

        I suppose my question is, is there a way for me to give carriage return in this system... command?