Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl script return value 2

Status
Not open for further replies.

CristianLuca

Programmer
Oct 25, 2007
36
RO
How can i emulate this functionality without printing in print_1 along with piping (print "$a|$b") and then using a pattern match to extract the results ? :

Obs : i don't mind piping the result into on string , i just don't want to put trash in STDIN or other file, i just need the result.

#script_1
my $a = "a";
my $b = "1";
# do something to return ($a,$b)

#script_2
my ($a,$b) = system("perl script_1");
print "$a\n";
print "$b\n";

output :
a
1

Thank you in advance,
Cristian

 
why not just use modules, and return the result instead of printing results?

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
The reson for why i decided to use my approach is because i have to run a script on a machine that logs on to [0-15] machines and runs a script there and then returns to my main script the results. Eny different ideas will be appreciated.

Cristian
 
the system() funtion does not return output from a program:

Code:
my ($a,$b) = system("perl script_1");

it returns the exit code/status of a program.

If you need to get some return values from a program use the qx// operator:

Code:
my @returned = qx/script.pl/;
print "$_\n" for @returned;



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Ok , thank you , it helped me understand this. One more thing how do i return something from the first script withought using print ?
i don't want to print/store the returned value to nowhere just return it.

Cristian
 
this will get the returned values you want:

my @returned = qx/script.pl/;

we use "print" in our examples just so you can see what the values are if you try the code. You do whatever you want with the values as required.


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
If you are dealing with another perl script, use do instead of a system call.

perldoc do said:
If do cannot read the file, it returns undef and sets $! to the error. If do can read the file but cannot compile it, it returns undef and sets an error message in $@ . If the file is successfully compiled, do returns the value of the last expression evaluated.

Note that inclusion of library modules is better done with the use and require operators, which also do automatic error checking and raise an exception if there's a problem.

However, I would strongly suggest that you create a module instead of any of these choices.

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top