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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

running unix commands in Perl ? 3

Status
Not open for further replies.

digpsc

Programmer
Mar 13, 2001
4
US
Hi, I have a Perl program and I want to execute another Perl Command in a different directory from my Perl Code.

How do I use system() to do this?
or
Am I supposed to use system()? if not, wat do i use?

any help would be greatly appreciated.
thanks,

pete
 
also, how can i get a result from the system to let me know if the call was successful ?
 
You could use the system command to do that, something like:
Code:
$retcode = system("/pathname/progname.pl");
$retcode /= 256;
That assumes the perl program you are calling has the correct path to perl in the #! line at the beginning.

You could also use backtics to do it:
Code:
@results = `/pathname/progname.pl`;
[code]
Same caveat about the #! line, but this way you can return more than just a return code, you can return a string, or even multiple lines.

However, probably the easiest way to do it is to use the [b]do[/b] command:
[code]
do "/pathname/progname.pl";
That doesn't require the #! line, and executes the program within the context of the current program. You can also wrap the do with an eval to trap any errors so they don't abort your program:
unless (eval {do "/pathname/progname.pl";}) {
print "eval failed, error $@\n";
}
[/code]
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
As is usually the case with Perl there is more than one way to do it. Choosing the best way depends on what you want to achieve. One way is to use 'system' to execute a system command (perl script, shell command, etc.) which displays the command's output and stores the command's exit status in the $? variable. Another way is to use backticks (``) which allow the command's output to be stored in a variable and also stores the command's exit status in the $? variable.

system("perlprogram"); # Output from command
if ($? != 0) {
print "perlprogram failed\n";
}


$output = `perlprogram`; # No output from command
if ($? != 0) {
print "perlprogram failed\n";
print "here is the output: \n";
print $output;
}
 
You can use "system", "exec", or backticks:

system - lets you execute a command, and gives you
back a return code. Here is an snippet from
"perldoc -f system":

@args = ("command", "arg1", "arg2");
system(@args) == 0
or die "system @args failed: $?"

exec - executes a system command, but does NOT
return. I haven't used this much - do
"perldoc -f exec" to read about this one.

backticks - execute a system command, and return
the output of the command. Example - I want
to run an "ls -l" command on the /tmp
directory:

@lines = `ls -l /tmp`;

the result is array @lines gets populated - each
element of the array will be populated with
one line of output from the command.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
wow,

thanks for the replys everyone.
I appreciate it !

I will try the backticks and the do/eval one. =D

I love your dragon quote Tracy hehe

thanks a lot everyone !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top