How can we make a system call within Perl

Hi,
How can we make a system call within Perl?

For example, I do the following in the command line. Now I wanna make the same in Perl.

java -jar myFirstcode.java input1 input2

Open in new window


Thanks,
TolgarAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
wilcoxonConnect With a Mentor Commented:
They are two different but similar solutions.

system("java -jar myFirstcode.java input1 input2");

or

$output  = `java -jar myFirstcode.java input1 input2`;

will both make the system call.  After doing either, you should do something like the following to catch errors (taken from perldoc -f system):
if ($? == -1) {
    print "failed to execute: $!\n";
} elsif ($? & 127) {
    printf "child died with signal %d, %s coredump\n",
    ($? & 127),  ($? & 128) ? 'with' : 'without';
} else {
    printf "child exited with value %d\n", $? >> 8;
}

Open in new window

0
 
TolgarAuthor Commented:
Also, let's say for some reason it cannot make the system call. How can I catch the exception in try catch?

Can you please send me an example code that is very safe in handling this exception?

Thanks,
0
 
sjklein42Connect With a Mentor Commented:
You can use the backtick operator:


$output  = `java -jar myFirstcode.java input1 input2`;

Open in new window

There is also a system() function that does pretty much the same thing.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
sjklein42Commented:
The exit status is available in $?

$output  = `java -jar myFirstcode.java input1 input2`;
$program_retval = $? >> 8;

Open in new window

0
 
TolgarAuthor Commented:
I did this. I think it is same as your first suggestion:

system("java -jar myFirstcode.java input1 input2");

Open in new window

But I didn't understand your second reply. Is it related with the second part of my question which is about catching exceptions?

Thanks,
0
 
sjklein42Commented:
Hi.

Using system is fine, too.

Yes, my second note had to do with how to examine the $? variable to determine the status of the command.  It applies to the system function as well as to the backtick operator.
0
 
wilcoxonCommented:
The advantage to backticks is that you can capture the output of the call.  If you don't need to do that, I would use system.
0
 
sjklein42Commented:
@wilcoxon is the expert on this.  What he say.
0
 
TolgarAuthor Commented:
Both solutions are very good but I picked wilcoxon's solution as best becasue it covers both questions and two different methods in a very descriptive way. But as I said both solutions are great.

Thanks,
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.