Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

call a program inside another program

by Anonymous Monk
on Sep 06, 2007 at 03:00 UTC ( #637311=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on call a program inside another program
Download Code
Re: call a program inside another program
by ikegami (Pope) 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.

      Thanks a lot
      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?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://637311]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (15)
As of 2014-10-01 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (38 votes), past polls