Home: Perl Programming Help: Intermediate:
Passing values between scripts



perlwatcher
User

Mar 16, 2010, 4:45 AM


Views: 3004
Passing values between scripts

Hi,

I wanted know if theres a way where we can pass values between two different perl scripts, like,

if there is a script with name "script1.pl" and another script2.pl in which it captures the return value from script1.pl


Code
 
my $value = [return value from script1.pl]


where $value contains the return value from script1.pl

Thanks


FishMonger
Veteran / Moderator

Mar 16, 2010, 5:31 AM


Views: 3001
Re: [perlwatcher] Passing values between scripts

Do you mean you want the exit code of script1.pl?

See: 'perldoc -f system'
http://perldoc.perl.org/functions/system.html


perlwatcher
User

Mar 16, 2010, 6:34 AM


Views: 2998
Re: [FishMonger] Passing values between scripts

No, Not the exit code. Its actually a return value.

I came across this need where I had to run one script which runs a Linux command continuously in a "while(1)" loop and another script which would capture the Process ID ( i.e. $$ in Perl context) and terminate it .

Hope this clarifies the question

Thanks


toolic
User

Mar 16, 2010, 10:05 AM


Views: 2991
Re: [perlwatcher] Passing values between scripts


Code
my $value = qx(script1.pl);


http://perldoc.perl.org/functions/qx.html


FishMonger
Veteran / Moderator

Mar 16, 2010, 10:31 AM


Views: 2987
Re: [perlwatcher] Passing values between scripts

If you mean that you want to capture the script's output, then you can use either the backticks operator or the qx() operator as toolic showed.


Code
my $value = `script1.pl`;



perlwatcher
User

Mar 17, 2010, 9:46 AM


Views: 2966
Re: [FishMonger] Passing values between scripts

I mean to say that if the script Script1.pl has a last statement in it like

return $value;

and that $value is caught in script2.pl

having script1.pl enclosed in backticks doesnt return me the value.


FishMonger
Veteran / Moderator

Mar 17, 2010, 10:13 AM


Views: 2963
Re: [perlwatcher] Passing values between scripts

The return statement is used inside of a subroutine, and is not necessarily the last line of that subroutine.

A return statement is not and can not be used used to pass data to another script.

The return value of a script is its exit code.

You can either trap the output of script1.pl or you can have script1 output the desired data into a file which script2.pl then reads that file to access the data.


perlwatcher
User

Mar 17, 2010, 8:35 PM


Views: 2957
Re: [FishMonger] Passing values between scripts

Thanks,

And what do u mean by trapping the output of the script1 , how do we do that?


MetalGear
Novice


Mar 18, 2010, 2:47 AM


Views: 2946
Re: [perlwatcher] Passing values between scripts

the part in script1.pl that returns value, make it a subroutine, say subvalue.

then you can use require function to get that value into script2.pl


Code
  

require "script1.pl";

$value = &subvalue;


perlwatcher
User

Mar 18, 2010, 5:03 AM


Views: 2937
Re: [anil mundewadi] Passing values between scripts

Putting the following code


Code
 
require "script1.pl";

$value = &function_from_script1.pl;


Will not return the value of the function in script1.pl, infact its running the entire function again.

I am pasting my two scripts here....



Code
 
my $procid;
sub procret {
while(1){

my @arr = `ls -al vsm-lun2.thpl`;
print $$,"\n";
my $procid = $$;

}
return $procid;
}

&procret;


And 2nd script is


Code
 
require "sig.pl";
my $ar1 = &procret;
kill(15,$ar1[1]);



perlwatcher
User

Mar 18, 2010, 5:09 AM


Views: 2934
Re: [perlwatcher] Passing values between scripts

Just FYI,

I was able to achieve the functionality I was trying using another means... but was curious to know whether if there is such a possibility...