Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Calling Perl from Perl

by icius (Sexton)
on Dec 04, 2002 at 14:09 UTC ( [id://217478]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to convert a bunch of perl scripts from Unix to Windows. Previously the scripts were called from a batch file in Unix, but I would like to use Perl exclusively for this. I have tried just using a backtick to call the scripts, but any environment variables I have set in the master script don't seem to propogate to the child scripts. Trying convert them all to modules/packages would require way more time than I have unless I misunderstand the complexity of modules. Is there an easy way to call Perl scripts from within a Perl script and keep the environment variables?

Replies are listed 'Best First'.
Re: Calling Perl from Perl
by iburrell (Chaplain) on Dec 04, 2002 at 18:08 UTC
    You should use system() instead of backticks. Backticks are for when you want to put the stdout of the program in a variable. Backticks and system will propagate environment variables on Windows.
Re: Calling Perl from Perl
by broquaint (Abbot) on Dec 04, 2002 at 14:25 UTC
    Is there an easy way to call Perl scripts from within a Perl script and keep the environment variables?
    I'm not sure how well this will work on your Window box but you could fork() then exec() your external scripts e.g
    @ENV{qw/ one two three /} = qw( foo bar baz ); exec "perl somescript.pl" if fork();
    Or you could just do() them in a separate package e.g
    { package RandomPkgName; do "somescript.pl"; }
    The package declaration will avoid cluttering up your current namespace.
    HTH

    _________
    broquaint

Re: Calling Perl from Perl
by Dog and Pony (Priest) on Dec 04, 2002 at 14:20 UTC
    Have a look at do (perldoc -f do).
    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.
Re: Calling Perl from Perl
by icius (Sexton) on Dec 04, 2002 at 14:44 UTC
    Sorry all. Forget it. I forgot that when printing from a program called using backtick that it does not send to the stdout of the master program. Once I printed the return value from the backtick I found that environment variables do indeed get propogated to the environment of the called program. Thanks for the lightning quick responses!!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://217478]
Approved by broquaint
Front-paged by tye
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-18 06:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found