perl - variable inheritance


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl - variable inheritance
# 1  
Old 03-27-2006
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
# 2  
Old 03-27-2006
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

Because $a is a package global (in package "main" here), its value is retained throughout the entire script unless its value is modified. Scoping has no effect, so you can always refer to it anywhere in the script.

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.

local() is okay, though, because it has a dynamic scope. Slightly better than unrestricted package global:


Code:
# cat a.pl
local $a = 6;
require 'b.pl';

# cat b.pl
print $a, "\n";

# perl -w a.pl
6

There's nothing wrong with passing parameters around. In fact, it is usually a bad idea to use package globals unless you have established different packages (namespaces) to avoid variable name clashes.

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  
Old 03-28-2006
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
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

What is wrong with below python inheritance code?

I am using python 3.4. Below is the exception I am getting- Traceback (most recent call last): File "./oop.py", line 20, in <module> y = DerivedClass("Manu") File "./oop.py", line 15, in __init__ super().__init__(self,value) TypeError: __init__() takes 2 positional arguments but... (2 Replies)
Discussion started by: Tanu
2 Replies

2. Red Hat

Ulimit Inheritance

Hi , If i start mysqld or httpd as root user which inturn starts them as "mysql" or "apache" user, will the ulimit of "root" user or ulimit of "mysql/apache" user be set for the mysql/apache processes. My understanding is that the ulimit of the user who initiates the process(root in this... (2 Replies)
Discussion started by: Hari_Ganesh
2 Replies

3. Programming

Difference in multiple inheritance and multilevel inheritance: same method name ambiguity problem

Hi, In multi-level inheritance: class A { public: void fun() { cout << "A" << endl; } }; class B : public A { public: void fun() { cout << "A" << endl; } }; class C : public B { }; int main() { C c; c.fun(); // Ans: A } (1 Reply)
Discussion started by: royalibrahim
1 Replies

4. Programming

Inheritance

whats the use of inheriting with access specifier as private..? Please specify the answer with a simple example... Regards -- Madhu (5 Replies)
Discussion started by: MadhuM
5 Replies

5. Shell Programming and Scripting

Inheritance in Perl

package Inventory_item; sub new { my($class) = shift; bless { "PART_NUM" => undef, "QTY_ON_HAND" => undef }, $class; } package main; $item = Inventory_item->new(); Can any one please help me in brief explaining the line... (5 Replies)
Discussion started by: parthmittal2007
5 Replies

6. Programming

C++ Inheritance problem??????

Hi friends, I hope u people are ok and doing fine. I have this small problem with the derived class. I have created te base class and there is a small problem with the definition of the derived class which the compiler is pointing out, could you please help me. Here is my code #ifndef... (2 Replies)
Discussion started by: gabam
2 Replies

7. AIX

WLM inheritance

Is none of you using WLM? A search gives no matches.... Anyway, I have set up a class with inheritance = yes. In the rules characteristics I have chosen a shell script as an application. This script is caught by the class, but not the child processes, which have the PID of the script as the... (6 Replies)
Discussion started by: firefox111
6 Replies

8. UNIX for Dummies Questions & Answers

Question on variable inheritance & code statements

1) I have the below code in concattxnrecords.sh shell script and it is calling the genericVars.sh shell script which is mentioned as below has some code inside it which would intialize some variables in it, now my question is will this shell script would inherit those variable definitions or not... (3 Replies)
Discussion started by: Ariean
3 Replies

9. Shell Programming and Scripting

Makefile: Parent - Child Inheritance and export

Hi, I have a number of Makefiles, including a couple of files that I include in Makefiles, a few scripts that are executed through Makefiles, and I have problems with environment variables that are not inherited to the scripts properly. Simplified scenario: rootdir/Makefile: all: ... (1 Reply)
Discussion started by: Shompis
1 Replies
Login or Register to Ask a Question