[Chicago-talk] Perl is telling me a HASH ref is saying a defined HASH ref ins undefined

Richard Reina richard at rushlogistics.com
Wed Jun 21 07:22:02 PDT 2023


With the code below I use Perl to search through old email with a given title before or after a certain date and delete them. I invoke it like this: perl -e 'use RIMAPtools; my ($IMLU_obj) = RIMAPtools->new(since => "17-Nov-2020", username => "richard",  domain => "rushlogistics.com", sub_stg => "NETWORK STATUS REPORT"); $IMLU_obj->look_up();'

Periodically the program crashes with error message: Can't use an undefined value as a HASH reference at /etc/perl/RIMAPtools.pm line 120.

  LINE 113:     if (! defined $imap->parse_headers($msg,"Subject")->{"Subject"}->[0]) {
  LINE 114:
  LINE 115:           print "msg SUBJECT was not defined. Skipping to next email.\n";
  LINE 116:           next;
  LINE 117:
  LINE 118:       } else {
  LINE 119:      
  LINE 120:            $msg_subject = $imap->parse_headers($msg,"Subject")->{"Subject"}->[0];
  LINE 121:
  LINE 122:       }


As you can see Perl tells me $imap->parse_headers($msg,"Subject")->{"Subject"}->[0] is defined on line 113 and then undefined on line 120. Any ideas?

Full subroutine below.

Thanks,

Richard

##################
sub look_up {
##################

     my ( $self, %args ) = @_;
    
     my ($svr, $pw) = get_cred($self->{_username}, $self->{_domain});

     print "Contacting: $svr vial Mail::IMAPClient.\n";

     my $userid =  $self->{_username} . '@' . $self->{_domain};
     
     my $imap=Mail::IMAPClient->new(

     Server => $svr,
     Port => 143,
     User => $userid,
     Password => $pw,
     Peek => 1,
     
    );
     
    unless($imap) { 
    
    die "Couldn't log in to server: $svr $@\n";

    }
    
    # Open the inbox
    $imap->select('INBOX');
    my @mails; 
    
    if ($self->{_since}) {

    print "Will look at messages before: $self->{_since}\n";
    @mails = $imap->since($self->{_since}) or die "Could not find any messages since $self->{_since}: $@\n";

    } elsif ($self->{_before}) {

    print "Will look at messages before: $self->{_before}\n"; 
    @mails = $imap->before($self->{_before}) or die "Could not find any messages before $self->{_before}: $@\n";
    
    }

     if ($self->{_sub_stg}) {

     print "Will search for mails with subject containing: $self->{_sub_stg}\n";

    }
     
    foreach my $msg (@mails) {

    my $msg_subject;

    if (! defined $msg) {

        print "msg was not defined. Skipping to next email.\n";
        next;
    }

    if (! defined $imap->parse_headers($msg,"Subject")->{"Subject"}->[0]) {

        print "msg SUBJECT was not defined. Skipping to next email.\n";
        next;

    } else {
        
        $msg_subject = $imap->parse_headers($msg,"Subject")->{"Subject"}->[0];

    }
    
    if ($self->{_sub_stg}) {
        
        print "Will look for string: " . $self->{_sub_stg} . " IN SUBJECT: " . $msg_subject . "\n";
        
        if ($msg_subject =~ /$self->{_sub_stg}/) {
        
        print "MATCH: " . $msg_subject . " DATE: " . $imap->get_header($msg,"Date") . "\n";
        $imap->delete_message($msg) or die "Could not delete_message: $@\n";;
                    
    }
        
    } # eo foreach
    
#######################    
} #EOS look_up
#######################


More information about the Chicago-talk mailing list