add error to an array or list or hash

M

monk

Hi all, and thanks in advance.

for example:

open (test.txt, /home/monk) or die $!;
blah blah blah

If it dies $!, How can I output/add that error to an array or hash or
list?
 
E

ebm

Hi all, and thanks in advance.

for example:

open (test.txt, /home/monk) or die $!;
blah blah blah

If it dies $!, How can I output/add that error to an array or hash or
list?

open(test.txt, /home/monk) or $myErrorHash->{'Error'} = $!;

Is that what your looking for. from there you can do something with
that hash and die later on.
 
E

ebm

open(test.txt, /home/monk) or $myErrorHash->{'Error'} = $!;

Is that what your looking for. from there you can do something with
that hash and die later on.

I wanted to put this

open(test.txt, /home/monk) or $myErrorHash{'Error'} = $!;
I goofed it the first time.
 
M

monk

open(test.txt, /home/monk) or $myErrorHash{'Error'} = $!;

Right on. Yeah..thanks.

I guess after I gather all the possible errors throughout the program,
I would like to fully identify each one of them uniquely.

Now in your experience, Do you recommend using arrays, hashes, or
lists? or just references {a=>b}?
 
E

ebm

Right on. Yeah..thanks.

I guess after I gather all the possible errors throughout the program,
I would like to fully identify each one of them uniquely.

Now in your experience, Do you recommend using arrays, hashes, or
lists? or just references {a=>b}?

It would really depend on what your doing. I normally just capture to
a string and pass it to subroutine that will log the problem then die.

open(.....) or Error("Didn't want to open: $!\n","ERROR") && die;
my Error sub would send out an email alert depending if it's a Warning
or Error and write to a logs file.
 
M

monk

open(.....) or Error("Didn't want to open: $!\n","ERROR") && die;
my Error sub would send out an email alert depending if it's a Warning
or Error and write to a logs file.

Thanks a lot ebm.
my Error sub would send out an email alert [..]
ooo..ahhhh....I like that.

My original idea was to create a hash or array with a pile of error
messages, reference the messages to the real $! error, and send that
variable value as a subject/body in an email.

But I'd like to see how your idea ties in all together. It seems
simpler.
Do you have that error subroutine you mention?

Thanks again.
 
E

ebm

open(.....) or Error("Didn't want to open: $!\n","ERROR") && die;
my Error sub would send out an email alert depending if it's a Warning
or Error and write to a logs file.

Thanks a lot ebm.
my Error sub would send out an email alert [..]

ooo..ahhhh....I like that.

My original idea was to create a hash or array with a pile of error
messages, reference the messages to the real $! error, and send that
variable value as a subject/body in an email.

But I'd like to see how your idea ties in all together. It seems
simpler.
Do you have that error subroutine you mention?

Thanks again.

This should set you up.
enjoy!

use Net::SMTP;
use Time::Local;
use File::path;
use Sys::Hostname;

$critical_email = '(e-mail address removed)';
$warning_email = '(e-mail address removed)';
$smtp='mail.example.com';
$from_email='(e-mail address removed)';

=pod

=item error($errorType, $subject, $description)

Checks to see if it's a criticality and send email message

Input: Error Type, Subject of Error, Description of Error
Return: True (1), False (0)

error("CRITICAL","Some Subject","Details");
error("WARNING","Some Subject","Details");

=cut

sub error{
my ($type,$event,$description) = @_;


chomp ($type, $event, $description);
logEvent("");
logEvent("$type\t$event \t$description\tServer: ".hostname);
print STDERR "$type\t$event \t$description\tServer: ".hostname;

if(uc($type) eq "CRITICAL"){
alertEmail( $critical_email,"LogMover CRITICAL \n$event","CRITICAL
$description\nServer: ".hostname);
return 1;
}elsif(uc($type) eq "WARNING"){
alertEmail($warning_email,"LogMover WARNING \n$event", "WARNING
$description\nServer: ".hostname);
return 1;
}
return 0;
}

=pod

=item checkPath($path)

check that the path exists, Return boolean


if( checkPath( $path ) ){ print "path exists" }

=cut

sub checkPath{
my($path) = @_; # directory path
#check the path exists
if( -e $path ){
return 1;
}
return 0;
}

=pod

=item logEvent($event)

Log an event to the harvester log, this will request the time for each
entry

Input: Event String
Return: True (1)

logEvent("Something to log");

=cut

sub logEvent{
my ($event) = @_; # event line for the log
chomp($event);

#setup log dir
checkPath("Logs") or mkpath("Logs");

my($month, $day, $year, $hrminsec) = todayDate(0);
my $LOG = 'Logs/'. $year.$month.$day . '.log';
open(LOG,">>$LOG" ) ||
die("CRITICAL". "unable to open $LOG","unable to open $LOG
$!");

print(LOG "$month/$day/$year $hrminsec\t ". $event . "\n");
close(LOG);
return 1;
}

=pod

=item alertEmail($toAddressFromConfig, $subject, $messageBody)

Email error message

Input: Address to send message to, Subject, Description of email
Return: N/A

alertEmail('(e-mail address removed)',"Some Subject","Details");

=cut

sub alertEmail{
my ($to,$subject,$messageBody) = @_;
chomp($to, $subject, $messageBody );


my $smtp = Net::SMTP->new($smtp, Debug => 0 );
$smtp->mail($from_email});
$smtp->to($to);
$smtp->datasend("\r\n");
$smtp->data();
$smtp->datasend("To: $to\n");
$smtp->datasend("From: ".$from_email."\n");
$smtp->datasend("Subject: $subject\r\n");
$smtp->datasend("\r\n");
$smtp->datasend("$messageBody\r\n");
$smtp->data();
$smtp->dataend();
$smtp->quit();
return 1;
}
 
G

Greg Bacon

: Hi all, and thanks in advance.
:
: for example:
:
: open (test.txt, /home/monk) or die $!;
: blah blah blah
:
: If it dies $!, How can I output/add that error to an array or hash or
: list?

Something like the following?

#! /usr/bin/perl

use warnings;
use strict;

my @errors;
for (qw( /foo/bar /baz/quux )) {
if (open my $fh, "<", $_) {
while (<$fh>) {
print if /42/;
}
}
else {
push @errors, "open $_: $!\n";
}
}

warn @errors if @errors;

Hope this helps,
Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top