CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



perlwatcher
User

Mar 16, 2010, 4:45 AM

Post #1 of 11 (1905 views)
Passing values between scripts Can't Post

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

Post #2 of 11 (1902 views)
Re: [perlwatcher] Passing values between scripts [In reply to] Can't Post

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

Post #3 of 11 (1899 views)
Re: [FishMonger] Passing values between scripts [In reply to] Can't Post

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

Post #4 of 11 (1892 views)
Re: [perlwatcher] Passing values between scripts [In reply to] Can't Post


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


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


FishMonger
Veteran / Moderator

Mar 16, 2010, 10:31 AM

Post #5 of 11 (1888 views)
Re: [perlwatcher] Passing values between scripts [In reply to] Can't Post

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

Post #6 of 11 (1867 views)
Re: [FishMonger] Passing values between scripts [In reply to] Can't Post

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

Post #7 of 11 (1864 views)
Re: [perlwatcher] Passing values between scripts [In reply to] Can't Post

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

Post #8 of 11 (1858 views)
Re: [FishMonger] Passing values between scripts [In reply to] Can't Post

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

Post #9 of 11 (1847 views)
Re: [perlwatcher] Passing values between scripts [In reply to] Can't Post

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

Post #10 of 11 (1838 views)
Re: [anil mundewadi] Passing values between scripts [In reply to] Can't Post

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

Post #11 of 11 (1835 views)
Re: [perlwatcher] Passing values between scripts [In reply to] Can't Post

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...

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives