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
-
1It's not at all clear what you actually need. Please elaborate. – Jonathan Hall Jan 03 '13 at 07: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) – gideon Jan 03 '13 at 09: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 – user1912265 Jan 04 '13 at 05:37
-
Sorry about the formattin. I'm doing it on my phone and i haven't got much formatting options available – user1912265 Jan 04 '13 at 05:39
2 Answers
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.
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.

- 42,728
- 8
- 80
- 84