Solved

Catching errors in system call in Perl?

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

          system("java -jar SOMEFILE.java");
          if ($? == -1) {
              #send email 
              sendEmail("$somename1@mywork.com", "somename2@mywork.com", "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("$somename1@mywork.com", "somename2@mywork.com", "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";
          close(MAIL);
      }

Open in new window

0
Comment
Question by:Tolgar
  • 3
  • 2
6 Comments
 
LVL 14

Accepted Solution

by:
DonConsolio earned 250 total points
Comment Utility
> sendEmail("$somename1@mywork.com", "somename2@mywork.com", "logging Failed", "failed to execute: $!\n");

omit the "$" before somename1
sendEmail("somename1@mywork.com", "somename2@mywork.com", "logging Failed", "failed to execute: $!\n");
0
 

Author Comment

by:Tolgar
Comment Utility
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.

Thanks,
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Do you want to backquote the @ or do you want to interpolate an array named @mywork?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 84

Expert Comment

by:ozo
Comment Utility
Or you may want to use q'' instead of qq""
0
 

Author Comment

by:Tolgar
Comment Utility
I don;t know what you mean by backquote but I want to send email to this email address: somename1@mywork.com

Then should I use \ before @ as the following?

somename1\@mywork.com

Thanks,
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 250 total points
Comment Utility

"somename1\@mywork.com"
or
'somename1@mywork.com'
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

10 Experts available now in Live!

Get 1:1 Help Now