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

Joel Berger joel.a.berger at gmail.com
Wed Jun 21 07:27:44 PDT 2023


I can't fully debug the result you're seeing without some sample data etc
(nor do I really have the time to do so) but what I will say is that you
probably want to store the results of parsing the headers in a variable and
not parse them several times. This could be just efficiency, but it also
might matter if the internals of the parser actually consume the data from
some buffer, something that is pretty typical of a parser. See if storing
those results into a temporary variable solves some of your issues.

Joel

On Wed, Jun 21, 2023 at 9:22 AM Richard Reina <richard at rushlogistics.com>
wrote:

> 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
> #######################
> _______________________________________________
> Chicago-talk mailing list
> Chicago-talk at pm.org
> https://mail.pm.org/mailman/listinfo/chicago-talk
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/chicago-talk/attachments/20230621/7caebb24/attachment-0001.html>


More information about the Chicago-talk mailing list