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

Hello, I am having trouble passing a variable to a function to use smtp. In the snippet below, the basic problem is that the $mailid variable is not being interpreted. Everything seems to be fine but no mail is sent. However, if a straight email address account is used instead of a variable, it works fine. Any ideas or help would be appreciated. Thanks!
use Net::SMTP; $var1="value1"; $var2="value2"; &sendm($var1,$var2); sub sendm() { my $emailid = $_[0]; my $subject = $_[1]; $smtp->to('$emailid\@address.com'); smtp->datasend("To: $mailid\@address.com\n"); }

Replies are listed 'Best First'.
Re: passing $var to smtp subroutine
by roboticus (Chancellor) on Feb 07, 2009 at 06:18 UTC
    csarid:

    Single quotes 'like this' prevent variables from being substituted. You want "double quotes".

    Untested example:

    my $foo='bar'; print "Foo is $foo\n"; print 'Foo is $foo\n';

    should print:

    Foo is bar Foo is $foo

    ...roboticus

    Update: I should've mentioned: The term used when a variable is substituted into a string is "interpolation".

Re: passing $var to smtp subroutine
by balakrishnan (Monk) on Feb 07, 2009 at 06:57 UTC
    line which is having the mistake :
    $smtp->to('$emailid\@address.com');
    proper line :
    $smtp->to("$emailid\@address.com");
    if you are using the single quotes for the string, then you
    have to use the eval to get value of variables used
    inside. Else you can go with double quotes.
      Balakrishnan, Thanks that is exactly what I was looking for. Although I should have experimented a bit more on this before submitting the question. Thanks again!
Re: passing $var to smtp subroutine
by AnomalousMonk (Archbishop) on Feb 07, 2009 at 07:32 UTC
    In addition, two things which will cause you problems later:
    • You are defining the function  sendm with a prototype that declares no arguments will be passed: the empty parentheses after the function name in
          sub sendm() { ... }
      Don't use prototypes until you are sure you know what they are for.
    • You are calling the function  sendm with the  & sigil. This disables prototype checking. Don't use this method of subroutine invocation until you are sure you know what it is for. Instead, call the subroutine just as
          sendm($var1,$var2);
      (but without having used a prototype – and certainly not an empty argument list prototype – in its definition).

    Update: Changed wording, layout to improve clarity.

      Thanks for the clarification. I removed the parenthesis so it looks like: "sub func" instead of "sub func()" and I did not have to use the &. Thanks again!
Re: passing $var to smtp subroutine
by toolic (Bishop) on Feb 07, 2009 at 13:39 UTC
    Do you really have 2 separate variables, $emailid and $mailid, or is $mailid a typo? Using the strictures will help catch potential mistakes like this:
    use warnings; use strict;
      Thanks toolic, I bit the bullet and put in the use warnings and strict and will used them moving forward