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

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.

Replies are listed 'Best First'.
Re: join email lists with comma unless one list is empty
by Perlbotics (Archbishop) on Jun 10, 2011 at 21:20 UTC

    Maybe?

    sub join_email_lists { return join(",", grep { length } @_); }

      Thank you. Very Perlish.

        This is the most perlish you can get :P
        sub join_email_lists { join ",", grep length, @_ }
Re: join email lists with comma unless one list is empty
by Your Mother (Archbishop) on Jun 11, 2011 at 18:04 UTC

    Just for fun, something a little more robust/checky. It can be made deeper to avoid obvious bounces and non-existent end points. See the doc: Email::Valid.

    use Email::Valid; use Carp qw( cluck ); sub join_email_lists { join ",", grep { Email::Valid->address($_) } @_; } sub join_email_lists_with_feedback { my @list; my @err; for my $address ( @_ ) { if ( Email::Valid->address($address) ) { push @list, $address; } else { push @err, $address; } } cluck "Problem address(es): ", join(", ", @err) if @err; join ",", @list; }
Re: join email lists with comma unless one list is empty
by wind (Priest) on Jun 10, 2011 at 21:22 UTC
    sub join_email_lists { return join ',', grep {$_} @_; }

      This method works even without the empty string at the beginning.

Re: join email lists with comma unless one list is empty
by davido (Cardinal) on Jun 10, 2011 at 22:28 UTC

    You shouldn't have to think about it. Perl has join, which automatically inserts the chosen character between list elements, but not before or after.

    sub join_lists { return join ',', @_; }

    Or if you want to get cute with double-quote interpolation:

    sub join_lists { local $" = ','; return "@_"; }

    Possibly I'm just not alert enough to spot why simple join isn't exactly what you're after, but it seems to me there's a simple answer.


    Dave

      ... why simple join isn't exactly what you're after ...

      Because Gulliver wants, for some reason, to have empty strings in the e-mail address list, and these need to be filtered out as Perlbotics and wind do.

        Does it say that somewhere in his question?

        Gah! I knew something was wrong with me. Up too late reading I guess. ;)


        Dave