madbombX has asked for the wisdom of the Perl Monks concerning the following question:

I am using Mail::Sender to send an email message and have used it a few times before but have hard coded the email address into the script. Now I am using Config::ApacheFormat (which I have also used before) for a config file. I know how to get an email address from the config file and use it in the code. However, I have a place where I am using an eval block to send an email. I have to assume (since I don't know better) that the email address is being interpolated. I think the way I want to fix it is to make that variable not be eval'd inside of the eval block. The corresponding lines in the config file are:
AdminEmail "Server Admin" <admin@server.com> AdminEmailCC "Tech Guys" <techs@server.com>

The corresponding code is (I know I am using 2 different methods to get the config lines, but I am just doing that to illustrate the flexibility I have with the code):

my $to = join(" ", $config->get("AdminEmail")); eval { (new Mail::Sender)->MailMsg( { smtp => "mail.server.com", from => "\"Postmaster\" <postmaster\@server.com>", to => $to, cc => $config->get("AdminEmailCC"), subject => "Email Subject", msg => "Email Body" }) or die "Error Sending Mail: $Mail::Sender::Error"; };

And the corresponding error message is: Odd number of elements in anonymous hash at my_script.pl line 454.

Can someone please explain to me what is happening here and how I can fix it?

Update: Thanks to merlyn and chromatic for setting me straight. It appears that in an eval block, the string is being called into an anonymous hash with an odd nuber of elements since the method is being called in a list context. The corrected section of code forces the string into a scalar context:

to => scalar join(" ", $config->get("AdminEmail")), cc => scalar join(" ", $config->get("AdminEmailCC")),

Replies are listed 'Best First'.
Re: Unevaling part of an eval block
by merlyn (Sage) on Nov 26, 2006 at 04:53 UTC
    cc => $config->get("AdminEmailCC"),
    What does that method call do in a list context? If it returns an even number of items, you're in for a world of hurt.

    Perhaps you really wanted:

    cc => scalar $config->get("AdminEmailCC"),
    which can't go wrong (at least in making the hash).
Re: Unevaling part of an eval block
by chromatic (Archbishop) on Nov 26, 2006 at 06:59 UTC
    I have to assume (since I don't know better) that the email address is being interpolated.

    You're thinking of eval STRING, which is very different from the eval BLOCK that you have here.

    By the way, you can get rid of the parentheses in:

    (new Mail::Sender)->MailMsg

    ... if you use the unambiguous method invocation syntax:

    Mail::Sender->new->MailMsg
Re: Unevaling part of an eval block
by roboticus (Chancellor) on Nov 26, 2006 at 13:50 UTC
    madbombX:

    You might also consider using single quotes on your from => ... line so you don't have to quote all the special characters...

    --roboticus

    Update: I corrected the first link, as I bumbled it good. (Thanks for point this out to me, Tanktalus!)