![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pass perl variable to sed | hi_ryo | Shell Programming and Scripting | 4 | 07-14-2008 07:16 AM |
| Pass csh variable to Perl | Raynon | Shell Programming and Scripting | 9 | 10-19-2007 06:46 PM |
| how to get type of variable in perl | umen | Shell Programming and Scripting | 2 | 07-26-2006 06:29 PM |
| Passing variable to perl | TheCrunge | Shell Programming and Scripting | 2 | 06-06-2006 01:43 PM |
| perl variable assingment | seismic_willy | Shell Programming and Scripting | 2 | 01-29-2002 01:54 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
perl - variable inheritance
Hey Everyone,
Does anyone know how - or if it's even possible - for a child perl script to inherit the variables of a parent perl script? In a shell script, you would use "export" for example. I am running Perl 5.8. Basically, let's say "perl1.pl" calls "perl2.pl" and I want "perl2.pl" to inherit the value of "$perl1_var" without passing it as a parameter. Any idea how to do this ? I've done some research and don't see this as even being an option. Thanks in advance for any help, Greg |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
That depends on what you mean by "parent" and "child" scripts.
If a Perl script invoked another Perl script through a shell with backticks, or system() for instance, then of course you can't. Even variables will not work as they are separate processes. Only some sort of IPC or command-line arguments will work. Probably you are not asking for this, but as a "parent" and "child" relationship, this seems to fit the terminology. The Perl require() doesn't really have much a parent or child relationship. It just means to load and execute the code in an external file, but the execution environment is just exactly the same one. Code:
~# cat a.pl $a = 6; require 'b.pl'; ~# cat b.pl print $a, "\n"; ~# perl a.pl 6 But this won't work because my imposes lexical scope: Code:
# cat a.pl my $a = 6; require 'b.pl'; # cat b.pl print $a, "\n"; # perl -w a.pl Use of uninitialized value in print at b.pl line 1. Code:
# cat a.pl local $a = 6; require 'b.pl'; # cat b.pl print $a, "\n"; # perl -w a.pl 6 Or you can use OOP. Then you probably can put everything into the object so that you only have one thing to pass around. |
|
#3
|
|||
|
|||
|
Thanks for the reply cbkihong. I've only started with perl about 4 months ago and haven't done much calling of one script from another, and even less of what I'm trying to do, which is basically have the same script invoke multiple instances of itself (it's a menu generator). I was hoping to give values to a set of variables once and make them available from any instance of the script.
Your reply is very thorough and greatly appreciated. And your solutions are beyond where I am with perl (yet!). I will most likely do this via 'ksh' where I can source script calls and export variables. Perl is a much better scripting language so it's unfortunate, but in this case, ksh is proving to be the better fit. thanks again cbkihong, greg |
|||
| Google The UNIX and Linux Forums |