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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |