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

I hope this hasn't been asked and answered. The question is:

In a string, say $x="jdoe@somewhere.com,bdoakes@nowhere.com";

How would I code a Perl script to make this string into a list of two usable EMail addresses (bearing in mind that the @ in this case is interpreted as a list variable introducer).

Replies are listed 'Best First'.
Re: Dealing with '@' in Text Strings
by choroba (Cardinal) on Sep 10, 2012 at 19:28 UTC
    I am not sure I undestand your question.

    First of all, you example does not work. Precisely for the reason you give: the @ in this case is interpreted as a list1 variable introducer. Use single quotes if you do not want variables to be interpolated in a string:

    $x = 'jdoe@somewhere.com,bdoakes@nowhere.com';
    To create a list1 of two e-mail addreses from this, you can use split:
    @addresses = split /,/, $x;

    1Or, to be exact, an array.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      I'm still making rookie mistakes, does it ever end? Thank you.

        I say that you know a programming language decently when you are able to write a non-trivial block of code and not have any syntax errors in it. More points if it works correctly on the first try, too :-)

Re: Dealing with '@' in Text Strings
by Kenosis (Priest) on Sep 10, 2012 at 20:17 UTC

    Consider using Email::Address:

    use Modern::Perl; use Email::Address; my $x = 'jdoe@somewhere.com,bdoakes@nowhere.com'; my @addresses = Email::Address->parse($x); say for @addresses;

    Output:

    jdoe@somewhere.com bdoakes@nowhere.com

    Update: Didn't notice that Anonymous Monk already covered this. I really do need more coffee...

Re: Dealing with '@' in Text Strings
by Rudolf (Pilgrim) on Sep 10, 2012 at 19:29 UTC

    To split this string into two seperate email values stored in an array, you can use the split() function which takes two arguemnts. The delimiter on which to seperate the original string ( in your case it looks like your using a comma ) and the string itself. To turn @ into a literal, use single quotes for $x or excape them with \@:

    my $x = 'jdoe@somewhere.com,bdoakes@nowhere.com'; my @list = split(/,/,$x); # /,/ specifies the delimiter
Re: Dealing with '@' in Text Strings
by Anonymous Monk on Sep 10, 2012 at 19:38 UTC
    That is supposedly some EMAIL cc/bcc thing, there are modules on CPAN to deal with that
    #!/usr/bin/perl -- use strict; use warnings; use Data::Dump 'pp'; use Email::Address; my $line = 'asdf@example.com,qwerty@example.com,bananas@example.com'; my @addresses = Email::Address->parse($line); pp $line, \@addresses; $_ .= "" for @addresses; # stringify pp $line, \@addresses; __END__ do { my $b = [ bless([ undef, "asdf\@example.com", "", "asdf\@example.com", [ \"asdf\@example.com,qwerty\@example.com,bananas\@example.com", 0, ], ], "Email::Address"), bless([ undef, "qwerty\@example.com", "", "qwerty\@example.com", ['fix', 1], ], "Email::Address"), bless([ undef, "bananas\@example.com", "", "bananas\@example.com", ['fix', 2], ], "Email::Address"), ]; $b->[1][4][0] = \${$b->[0][4][0]}; $b->[2][4][0] = \${$b->[0][4][0]}; ( "asdf\@example.com,qwerty\@example.com,bananas\@example.com", $b, ); } ( "asdf\@example.com,qwerty\@example.com,bananas\@example.com", [ "asdf\@example.com", "qwerty\@example.com", "bananas\@example.com", ], )