[Chicago-talk] Perl is telling me a HASH ref is saying a defined HASH ref ins undefined
Alan Mead
amead at alanmead.org
Wed Jun 21 11:29:19 PDT 2023
Richard,
Does defined() do what you want? I think you might want line 113 to be:
if (! *exists* $imap->parse_headers($msg,"Subject")->{"Subject"} || !
*exists* $imap->parse_headers($msg,"Subject")->{"Subject"}->[0] || !
defined $imap->parse_headers($msg,"Subject")->{"Subject"}->[0]) {
I have been surprised by what I think is called autovivication. I could
also well be wrong about this.
-Alan
On 6/21/2023 9:22 AM, Richard Reina 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
--
Alan D. Mead, Ph.D.
President, Talent Algorithms Inc.
science + technology = better workers
https://talalg.com
The reasonable man adapts himself to the world: the unreasonable one
persists in trying to adapt the world to himself. Therefore all progress
depends on the unreasonable man.
-- Shaw, from "Maxims for Revolutionists"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/chicago-talk/attachments/20230621/23c0aa6a/attachment-0001.html>
More information about the Chicago-talk
mailing list