The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-27-2006
Registered User
 

Join Date: Jun 2002
Location: Irvine, CA
Posts: 21
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
Reply With Quote
Forum Sponsor
  #2  
Old 03-27-2006
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,477
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.
Reply With Quote
  #3  
Old 03-28-2006
Registered User
 

Join Date: Jun 2002
Location: Irvine, CA
Posts: 21
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:43 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0