Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

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,
Avatar of Tolgar
Tolgar

ASKER

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,
SOLUTION
Avatar of sjklein42
sjklein42
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The exit status is available in $?

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

Open in new window

Avatar of Tolgar

ASKER

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,
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.
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
@wilcoxon is the expert on this.  What he say.
Avatar of Tolgar

ASKER

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,