The attached code will join two email lists with a comma between them but if either is empty then no comma is added and if both are empty then '' is returned.

Is there a better or more Perlish way to do this? I just confirmed MIME::Lite doesn't care if there is an extra comma in front or back but I have a subroutine that returns if the address field is empty without attempting to send.

#!/usr/bin/perl use strict; use warnings; my $list1 = 'jj@xyz.com,kk@xyz.com'; my $list2 = 'aa@xyz.com'; print join_email_lists( $list1,$list2),"\n"; #want comma separated print join_email_lists( '' ,$list2),"\n"; #no comma in front print join_email_lists( $list1,'' ),"\n"; #no comma behind print join_email_lists( '' ,'' ),"\n"; #want '' sub join_email_lists { my ($a1,$a2) = @_; return $a1 . ',', $a2 if $a1 and $a2; return $a1 if $a1; return $a2 if $a2; return ''; }

Update: The email lists come from a config file in my production code and I wanted to make sure it still works if someone clears one or both email list. I tried to simplify things but leaving the long names $error_only_list and $email_always_list would have made things clearer.

When I put this code into production it would only return the first list. Then I noticed I had a comma where it should have been a '.' in the first return statement.


In reply to join email lists with comma unless one list is empty by Gulliver

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.