<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Richard,</p>
<p>Does defined() do what you want? I think you might want line 113
to be:<br>
<br>
if (! <b>exists</b>
$imap->parse_headers($msg,"Subject")->{"Subject"} || ! <b>exists</b>
$imap->parse_headers($msg,"Subject")->{"Subject"}->[0] ||
! defined
$imap->parse_headers($msg,"Subject")->{"Subject"}->[0]) {<br>
</p>
<div class="moz-cite-prefix">I have been surprised by what I think
is called autovivication. I could also well be wrong about this.</div>
<div class="moz-cite-prefix"><br>
</div>
<div class="moz-cite-prefix">-Alan</div>
<div class="moz-cite-prefix"><br>
</div>
<div class="moz-cite-prefix">On 6/21/2023 9:22 AM, Richard Reina
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:1687357322.fxgra9daaswsgwcg@hostingemail.digitalspace.net">
<pre class="moz-quote-pre" wrap="">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
<a class="moz-txt-link-abbreviated" href="mailto:Chicago-talk@pm.org">Chicago-talk@pm.org</a>
<a class="moz-txt-link-freetext" href="https://mail.pm.org/mailman/listinfo/chicago-talk">https://mail.pm.org/mailman/listinfo/chicago-talk</a>
</pre>
</blockquote>
<pre class="moz-signature" cols="72">--
Alan D. Mead, Ph.D.
President, Talent Algorithms Inc.
science + technology = better workers
<a class="moz-txt-link-freetext" href="https://talalg.com">https://talalg.com</a>
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"
</pre>
</body>
</html>