From richard at rushlogistics.com Wed Jun 21 07:22:02 2023 From: richard at rushlogistics.com (Richard Reina) Date: Wed, 21 Jun 2023 10:22:02 -0400 Subject: [Chicago-talk] Perl is telling me a HASH ref is saying a defined HASH ref ins undefined Message-ID: <1687357322.fxgra9daaswsgwcg@hostingemail.digitalspace.net> 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 ####################### From joel.a.berger at gmail.com Wed Jun 21 07:27:44 2023 From: joel.a.berger at gmail.com (Joel Berger) Date: Wed, 21 Jun 2023 09:27:44 -0500 Subject: [Chicago-talk] Perl is telling me a HASH ref is saying a defined HASH ref ins undefined In-Reply-To: <1687357322.fxgra9daaswsgwcg@hostingemail.digitalspace.net> References: <1687357322.fxgra9daaswsgwcg@hostingemail.digitalspace.net> Message-ID: 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 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: From richard at rushlogistics.com Wed Jun 21 07:38:59 2023 From: richard at rushlogistics.com (Richard Reina) Date: Wed, 21 Jun 2023 10:38:59 -0400 Subject: [Chicago-talk] Perl is telling me a HASH ref is saying a defined HASH ref ins undefined In-Reply-To: References: <1687357322.fxgra9daaswsgwcg@hostingemail.digitalspace.net> Message-ID: <1687358339.f8e6xg4d34w8s4ok@hostingemail.digitalspace.net> Joel, Thank you very much for the quick reply. I'll look into putting it in an array and see if that helps. Thanks again! ? On Wed, 21 Jun 2023 09:27:44 -0500, Joel Berger wrote: ? 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 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 _______________________________________________ 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: From amead at alanmead.org Wed Jun 21 11:29:19 2023 From: amead at alanmead.org (Alan Mead) Date: Wed, 21 Jun 2023 13:29:19 -0500 Subject: [Chicago-talk] Perl is telling me a HASH ref is saying a defined HASH ref ins undefined In-Reply-To: <1687357322.fxgra9daaswsgwcg@hostingemail.digitalspace.net> References: <1687357322.fxgra9daaswsgwcg@hostingemail.digitalspace.net> Message-ID: <55e9cf41-b744-94bc-b77f-b5b7fac3e925@alanmead.org> 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: