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

Hi Monks,

In the following split on an email address, if I do not want the first part of the string, what is the best way to let Perl know this? I thought maybe undefined would work, but Perl did not like that. Should I just assign it to a temp variable and not use it again? I would think this is bad practice.
my $domain; (undefined,$domain) = split(/\@/,$email); print "Domain is $domain\n";
Thanks.

Replies are listed 'Best First'.
Re: Splitting a string, Just want to Keep Half
by hardburn (Abbot) on Jul 06, 2003 at 00:56 UTC

    What you really want is to use Mail::Address:

    use Mail::Address; my $addr = Mail::Address->parse('user@example.com'); my $domain = $addr->host();

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: Splitting a string, Just want to Keep Half
by Zaxo (Archbishop) on Jul 06, 2003 at 00:15 UTC

    You're sooo close: (undef, my $domain) = split /\@/, $email; or else: my $domain = (split /\@/, $email)[1];

    Update: Pay attention to Limbic~Region and hardburn, email addresses are more than your code or mine admits. I only corrected your syntax.

    After Compline,
    Zaxo

Re: Splitting a string, Just want to Keep Half
by ctilmes (Vicar) on Jul 06, 2003 at 00:11 UTC
    my $domain = (split(/\@/,$email))[1]; print "Domain is $domain\n";
Re: Splitting a string, Just want to Keep Half
by broquaint (Abbot) on Jul 06, 2003 at 00:11 UTC
    Just use a slice
    my $domain = (split '@', 'dru@perlmonks.org')[-1]; print "Domain is $domain\n"; __output__ Domain is perlmonks.org

    HTH

    _________
    broquaint

Re: Splitting a string, Just want to Keep Half
by Limbic~Region (Chancellor) on Jul 06, 2003 at 00:28 UTC
    Dru,
    It is commonly thought that a valid email address is far more strict than it actually is. I think email addresseses may even contain more than one @ symbol. This would imply using the limit argument in the split. I do believe there are some fairly decent modules on CPAN for email address stuff if this is going to be used for anything other than just casual personal use.

    Ok - chances are you didn't need to hear any of that, so an alternative to split would be to use index and substr.

    #!/usr/bin/perl -w use strict; my $email = 'joe.snuffy@nowhere.com'; my $domain = substr($email,index($email,'@') + 1); print "Domain is $domain\n";

    Cheers - L~R

Re: Splitting a string, Just want to Keep Half
by Aristotle (Chancellor) on Jul 06, 2003 at 12:49 UTC
    Also note that since you're not actually looking to split the string, this might better be expressed as a pattern match: "I want the part after the @ sign" rather than "I want the second of the string parts delimited by an @ sign".
    my ($domain) = ($email =~ /@(.*)/); print "Domain is $domain\n";

    Makeshifts last the longest.

      I would also recommend to use a regex instead of a split as it allows you to check for valid domain name characters :
      my ($domain) = ($email =~ /@([a-zA-Z0-9.-]+)$/); if( defined $domain ) { print "Domain is $domain\n"; } else { print "Invalid or missing domain name ...\n"; }
Re: Splitting a string, Just want to Keep Half
by David Caughell (Monk) on Jul 06, 2003 at 22:22 UTC
    Hey!

    I'm a bit of a newb to this language (though I'm ambitious... currently I'm working on writing a script that makes PHP conform to "use strict;", in a limited sense). :)

    I think that the people who've written comments on this so far have done some beautiful code, which is true to the intent of Perl, but isn't necessarily what's required for a beginner working on this project.

    If I were you, (and I wanted to follow this line of thinking in terms of splitting the email addy into an array) I would do the following:

    use strict; use warnings; # as always! my $emailaddy = "grasp_the_sun\@notme.com"; my @emailarr = split /\@/, $emailaddy; my $domain = pop @emailarr;
    Just to give you an idea of how new to the language I am, I tested my original code, and I'd forgotten that I needed to preceed the "@" in the email address with a "\". But the code does work as presented.

    Hope this helps!
    --Dave.
Re: Splitting a string, Just want to Keep Half
by TomDLux (Vicar) on Jul 07, 2003 at 00:16 UTC

    defined is a verb, to detect whether a scalar has a value.

    undef is a noun, to assign a scalar not have a value.

    In the code below, some values are true, some false, but all are defined. Then, assigning undef to them means they are no longer defined. Notice that the hash still contains the key a, with no value associated with it; you would need to delete the key to remove the key from the hash.

Re: Splitting a string, Just want to Keep Half
by rajiv7374 (Initiate) on Jul 07, 2003 at 06:04 UTC
    #!/opt/perl/bin/perl my $domain; $email=q{rajiv.gangadharan@hp.com}; ($domain) = (split /\@/,$email)1; print "Domain is $domain\n"; Just take the first (exclude the 0th element which is the name) element and print it.