Solved

How can we make a system call within Perl

Posted on 2011-03-16
9
277 Views
Last Modified: 2012-05-11
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,
0
Comment
Question by:Tolgar
  • 4
  • 3
  • 2
9 Comments
 

Author Comment

by:Tolgar
ID: 35153239
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
 
LVL 16

Assisted Solution

by:sjklein42
sjklein42 earned 250 total points
ID: 35153248
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
 
LVL 16

Expert Comment

by:sjklein42
ID: 35153270
The exit status is available in $?

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

Open in new window

0
 

Author Comment

by:Tolgar
ID: 35153355
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:sjklein42
ID: 35153423
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
 
LVL 26

Accepted Solution

by:
wilcoxon earned 250 total points
ID: 35153440
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
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35153442
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
 
LVL 16

Expert Comment

by:sjklein42
ID: 35153460
@wilcoxon is the expert on this.  What he say.
0
 

Author Closing Comment

by:Tolgar
ID: 35153507
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now