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

I have a piece of code, where some value ( an email address ) that comes from the db using the ORM module Class::DBI. See below

 my $email = $member->email # email address is fetched from the db. say for ex: peter@gmail.com

Now I want to split the username and domain from the email address. But since @ is a special character when I try to split the string $email with '@', its not working as expected. I think Perl already interpolates it while assigning the value to $email. For easy description, see the below code and help me to fix it.

my $email = "peter@gmail.com"; # In real case its from $member->email +. So don't ask me to replace this with "peter\@gmail.com" or 'peter@g +mail.com' my ($u,$d) = split (/@/,$email); print "$u,$d\n";

Replies are listed 'Best First'.
Re: Disable Interpolation while assigning a value to a variable
by soonix (Chancellor) on Jun 26, 2024 at 12:05 UTC
    In real case its from $member->email. So don't ask me to replace this with "peter\@gmail.com" or 'peter@gmail.com'
    your $member->email contains the address WITH QUOTES??
    Please print ">>>" . $member->email . "<<<\n"; to be sure.
    Otherwise, use single quotes for addresses that don't come from the database, see Quote and Quote like Operators.
Re: Disable Interpolation while assigning a value to a variable
by BillKSmith (Monsignor) on Jun 26, 2024 at 18:11 UTC
    Are you using use strict and use warnings? Note that even your sample code fails to compile under strict. If you are using them and are not getting any messages, it is unlikely that your problem has anything to do with unintended interpolation.
    Bill
Re: Disable Interpolation while assigning a value to a variable
by LanX (Saint) on Jun 26, 2024 at 12:21 UTC
    As soonix was already indicating, the question doesn't make sense.

    Interpolation only happens within "double quote" 'ish contexts

    The content of $member->email should always be safe, unless you are doing some weird eval

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

Re: Disable Interpolation while assigning a value to a variable
by Anonymous Monk on Jun 26, 2024 at 13:08 UTC

    I believe the OP is asking about the split(), which needs to be coded my ( $u, $d ) = split /\@/, $email;

      That backslash is not required.

      #!perl use strict; use warnings; my $email = 'peter@gmail.com'; my ($u,$d) = split (/@/,$email); print "$email => $u,$d\n";

      outputs:

      peter@gmail.com => peter,gmail.com

      In other words, if $email properly contains the email address, the split command that the original poster showed works as they wanted it to -- without your unneeded backslash (though your backslash doesn't change the results, in this case). Hence, the earlier monks were properly digging into the actual contents of $email to try to debug the problem.