Catching errors in system call in Perl?

Posted on 2011-03-22
Last Modified: 2012-06-27
Is there anything wrong in this code? Especially on line 4 and 11.

          system("java -jar");
          if ($? == -1) {
              #send email 
              sendEmail("$", "", "logging Failed", "failed to execute: $!\n");
          } elsif ($? & 127) {
              printf "child died with signal %d, %s coredump\n",
                ($? & 127),  ($? & 128) ? 'with' : 'without';
              my $signal = ($? & 127);
              my $woCoredump = ($? & 128) ? 'with' : 'without';
              #send email
              sendEmail("$", "", "Logging Failed", "child died with signal $signal, $woCodedump coredump\n");
          } else {
              # Don't do anything because it is a successful system call.

Open in new window

and this is the sendEmail sub function

# Email Function
    #($to, $from, $subject, $message)
    sub sendEmail
          my ($to, $from, $subject, $message) = @_;
          my $sendmail = '/usr/lib/sendmail';
          open(MAIL, "|$sendmail -oi -t");
          print MAIL "From: $from\n";
          print MAIL "To: $to\n";
          print MAIL "Subject: $subject\n\n";
          print MAIL "$message\n";

Open in new window

Question by:Tolgar
  • 3
  • 2
LVL 15

Accepted Solution

DonConsolio earned 250 total points
ID: 35190083
> sendEmail("$", "", "logging Failed", "failed to execute: $!\n");

omit the "$" before somename1
sendEmail("", "", "logging Failed", "failed to execute: $!\n");

Author Comment

ID: 35191137
Sounds good. So is this rest of my code is right?

I can't run and check it for now that is why I am asking.

LVL 84

Expert Comment

ID: 35192138
Do you want to backquote the @ or do you want to interpolate an array named @mywork?
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LVL 84

Expert Comment

ID: 35192150
Or you may want to use q'' instead of qq""

Author Comment

ID: 35192263
I don;t know what you mean by backquote but I want to send email to this email address:

Then should I use \ before @ as the following?


LVL 84

Assisted Solution

ozo earned 250 total points
ID: 35192595


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
unable to remove invisible characters with perl 2 89
Perl modules on linux ec2 3 113
Call Shell Script from Perl Script 6 133
Perl tar error 8 66
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

809 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