add error to an array or list or hash

Discussion in 'Perl Misc' started by monk, Sep 26, 2007.

  1. monk

    monk Guest

    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?
     
    monk, Sep 26, 2007
    #1
    1. Advertising

  2. monk

    ebm Guest

    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.
     
    ebm, Sep 26, 2007
    #2
    1. Advertising

  3. monk

    ebm Guest

    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.
     
    ebm, Sep 26, 2007
    #3
  4. monk

    monk Guest

    > 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}?
     
    monk, Sep 26, 2007
    #4
  5. monk

    ebm Guest

    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.
     
    ebm, Sep 26, 2007
    #5
  6. monk

    monk Guest

    > 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.
     
    monk, Sep 26, 2007
    #6
  7. monk

    ebm Guest

    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 = '';
    $warning_email = '';
    $smtp='mail.example.com';
    $from_email='';

    =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('',"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;
    }
     
    ebm, Sep 27, 2007
    #7
  8. 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/"
     
    Tad McClellan, Sep 27, 2007
    #8
  9. monk

    Greg Bacon Guest

    In article <>,
    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
     
    Greg Bacon, Sep 27, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    539
    red floyd
    Nov 10, 2011
  2. Anthony Martinez
    Replies:
    4
    Views:
    275
    Robert Klemme
    Jun 11, 2007
  3. Michal Suchanek
    Replies:
    6
    Views:
    236
    Nobuyoshi Nakada
    Jun 13, 2007
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    627
    David A. Black
    Jul 2, 2008
  5. Replies:
    11
    Views:
    190
    Eric Schwartz
    Oct 10, 2005
Loading...

Share This Page