-3

I need to call a perl script from another script. The constraint is that, few global variables defined in a .pm file is common between the two scripts but the local variables are not shared. Please let me know how to do this

4
  • 1
    It's not at all clear what you actually need. Please elaborate. Commented Jan 3, 2013 at 7:33
  • You need to include some code!! When you do feel like sharing your code, please edit your question and update it. Notify one of us to then reopen your question. We can help you if need assistance with formatting or anything. (Just put your relevant code and we'll format it for you) Commented Jan 3, 2013 at 9:15
  • sorry for not completing my question. Sample code is as below: caller.pl use my::module; $global_var = 1; #need to change this system("called.pl"); print $global_var; called.pl use my::module; $global_var=2; i want the output of running caller.pl to be 2. Please note that global_var is in my::module Commented Jan 4, 2013 at 5:37
  • Sorry about the formattin. I'm doing it on my phone and i haven't got much formatting options available Commented Jan 4, 2013 at 5:39

2 Answers 2

3

Is there any reason you cant call the code from the second script as a function from the first script? It should work as a single program and thus share global variables.

Check How do I include functions from another file in my Perl script? for the various ways to include one script into another.

Sign up to request clarification or add additional context in comments.

Comments

0

It depends how you want this to work.

If you want to share global variables between processes then you are in for a "treat". You will need something like shared-memory (shmget) or some other IPC (Inter-Process Communication) mechanism, see perldoc perlipc. The fun part is is synchronising the two processes so they don't try to update the same thing at the same time. You really don't want to go there if you can avoid it.

Simpler is if you just want to pass those global values to the second process so it can take its own copy. If the values are simple text then you can just pass them as command-line parameters (read them from @ARGV) or as environment variables (%ENV). However, if they are more exotic, like references or file handles, then you need to figure out a different protocol. Creating the child process using fork (you might not need exec) might be a simpler way to go assuming you are not using Windows.

Better yet, consider if you really do need to copy or share those globals.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.