add error to an array or list or hash
add error to an array or list or hash
am 26.09.2007 21:37:45 von rsarpi
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?
Re: add error to an array or list or hash
am 26.09.2007 22:01:24 von ebm
On Sep 26, 12:37 pm, monk wrote:
> 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.
Re: add error to an array or list or hash
am 26.09.2007 22:03:00 von ebm
On Sep 26, 1:01 pm, ebm wrote:
> On Sep 26, 12:37 pm, monk wrote:
>
> > 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.
I wanted to put this
open(test.txt, /home/monk) or $myErrorHash{'Error'} = $!;
I goofed it the first time.
Re: add error to an array or list or hash
am 26.09.2007 22:30:32 von rsarpi
> 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}?
Re: add error to an array or list or hash
am 26.09.2007 22:44:42 von ebm
On Sep 26, 1:30 pm, monk wrote:
> > 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}?
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.
Re: add error to an array or list or hash
am 26.09.2007 23:56:18 von rsarpi
> 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.
Re: add error to an array or list or hash
am 27.09.2007 01:33:41 von ebm
On Sep 26, 2:56 pm, monk wrote:
> > 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 = 'critical@example.com';
$warning_email = 'warning@example.com';
$smtp='mail.example.com';
$from_email='user@example.com';
=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('to@example.com',"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;
}
Re: add error to an array or list or hash
am 27.09.2007 02:16:16 von Tad McClellan
monk wrote:
> open (test.txt, /home/monk) or die $!;
You should put quotes around strings in Perl programs.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
Re: add error to an array or list or hash
am 27.09.2007 23:48:25 von gbacon
In article <1190835465.990643.167840@w3g2000hsg.googlegroups.com>,
monk wrote:
: 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
--
The possession of arms is the distinction between a freeman and a slave.
-- James Burgh