Re: $msg->sender bug? Or bad usage?

Mark Overmeer <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.mail-box
Organization MARKOV Solutions
Message-ID <[email protected]>
* Adam Augustine ([email protected]) [040121 01:22]:
> I have a script that catches bounces from a mailing list. Some of the 
> bounces are very, very odd which tend to tickle bugs in my code.

code:

> @from = $msg->sender;
> print "\$#from is: $#from\n";
> print "\$from is: $from[0]\n";

output:

> $#from is: 0
> Use of uninitialized value in concatenation (.) or string at 
> ./testscript.pl line 11, <STDIN> line 1.
> $from is:

> My understanding is that if an array is empty then $#array should be -1. 
> It seems to me that @from = $msg->sender; seems to be doing something 
> very strange to @from, making all values none-existent, but changing the 
> index.

The answer is simple, but sometimes we get blinded when we focus too
much on what we expect.  $msg->from, ->to, ->cc, and ->bcc all return
lists of addresses, because the RFCs say that there can be more than
one address. $msg->sender returns (as documented) only ONE address.
So, in case there is no "Sender:" or "From:" field, the effective
action is  @from = (undef);

The best way to use sender:

  if(my $sender = $msg->sender)
  {   ....
  }


> my @from = $msg->from;
> my @from_address_list = ();
> if ($#from >= 0) {
>          foreach (@from) {
>                  push (@from_address_list, $_->address);
>          }
> }

This piece of code can be simplified a lot. Remember that a foreach with an
empty list is simply skipped.  So, this code become:

  my @from = $msg->from;
  my @from_address_list = ();
  foreach (@from) {
     push @from_address_list, $_->address;
  }

or

  my @from_address_list;
  foreach ($msg->from) {
     push @from_address_list, $_->address;
  }

or

  my @from_address_list = map { $_->address } $msg->from;

-- 
               MarkOv

------------------------------------------------------------------------
drs Mark A.C.J. Overmeer                                MARKOV Solutions
       [email protected]                          [email protected]
http://Mark.Overmeer.net                   http://solutions.overmeer.net
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.