http://www.perlmonks.org?node_id=637311

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

Hi all,
I have a perl program and in that I am calling another perl program an +d passing some parameters. How can I do that? ======================= program1.pl #!/usr/bin/perl print "hello"; program2.pl "username" "userid" #How can i call program2.pl using the above parameters? ======================== Thanks

Replies are listed 'Best First'.
Re: call a program inside another program
by ikegami (Patriarch) on Sep 06, 2007 at 04:17 UTC

    There are different ways of calling other programs depending on whether the child should run in parallel or not, whether the child's output should be captured or not, etc.

    If you want to wait for the child program to finish executing before continuing, and you don't need to capture it's output or feed anything to it's STDIN, then you can use system.

    Windows: system('program2.pl "username" "userid"'); Elsewhere: system('program2.pl', 'username', 'userid');

    Refer to system's documentation for more info including how to check if an error occured.

      You can also call system with a list on Win32:

      C:\>perl -e "system(qw/skript.pl 1 2 3/)" 1 2 3

        But system(LIST) is implemented as a barely disguised system(STRING). The below example tries to pass a string containing double quotes ('4 " and " 5') to a child Perl interpreter which prints out its @ARGV:

        Q:\>perl -e "$q=chr(34);system($^X, '-le', 'print for @ARGV', 1, 2, 3, +qq(4 $q and $q 5))" 1 2 3 4 and 5

        There would be ways to make system(LIST) saner but as the underlying OS call basically is system(STRING) and there is no central library routine for or consensus on deparsing the command line, and even the various MSVCRTs differ in how they deparse the command line, it's kinda hard.

        In Linux OS if I use code system('program2.pl', 'username', 'userid'); How can I catch those values in program2.pl?
      Thanks a lot