Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Strange Situation with Net::SMTP

by CodeJunkie (Monk)
on Mar 25, 2003 at 11:56 UTC ( #245657=perlquestion: print w/replies, xml ) Need Help??

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

I have a strange situation with trying to use

When I try to connect using

use Net::SMTP; my $smtp_server=''; my $smtp = Net::SMTP->new($smtp_server) or die "Failed:$!";
It does not work, but when I don't use a variable and just put
my $smtp = Net::SMTP->new('') or die "Failed:$!" +;
It seems to work ok.

Am I being dense here...? Can anyone tell me the difference between putting things in " " and ' ' and not at all?

i.e. my $value="this"; or my $value='that';

I've also been wandering the best way to define a hash, i.e. $HASH{'key'} or $HASH{key} ??

Sorry to ask so many questions at the same time!


Replies are listed 'Best First'.
Re: Strange Situation with Net::SMTP
by Thelonius (Priest) on Mar 25, 2003 at 15:00 UTC
    Like shotgunefx, I don't see any reason why your first code snippet wouldn't work.

    To answer the one question he didn't, inside double quotes variables are substituted and backslash-escapes are processed. Inside single quotes, they are not. So:

    $a = "fred"; print "$a\n"; # prints fred print '$a\n'; # prints $a\n
    See Quote and Quote-like Operators.
Re: Strange Situation with Net::SMTP
by shotgunefx (Parson) on Mar 25, 2003 at 13:30 UTC
    Odd. Not sure why it's not working. I'd add a
    print $smtp_server,"\n";
    on the line before calling new to make sure the lexical $smtp_server is still in scope. Also "use warnings;"!

    As far as the $HASH{'KEY'} it's redundant for the most part as hash keys are automatically "stringified" (You need to use them if the key name has a dash though 'key-name' or would otherwise be interperated as a compound statement). I personally use 'key' but that's just for the editor I use to get it to syntax highlight the hash keys.


    "To be civilized is to deny one's nature."
Re: Strange Situation with Net::SMTP
by pg (Canon) on Mar 25, 2003 at 17:54 UTC
    Donít see a reason why your code does not work, believe your code is not the exact copy of your original one.

    As for the difference between ď and Ď, I made this demo for you. To play with it, first replace the pseudo email server and address with your real ones, then uncomment one of the four commented lines, and see what happens. Explanations are beside those lines.

    use Net::SMTP; $smtp = Net::SMTP->new(''); $smtp->mail(''); #$smtp->to(''); #this one works, assume exists #$smtp->to(""); #this one doesn't work, as Perl will try to r +esolve @abc for you #$smtp->to('p\'); #this one does not work, as Perl see the add +ress as p\ (assume that this address does not exist) #$smtp->to("p\"); #this one works, as we escaped that @ $smtp->data(); $smtp->datasend("To: Peter\n"); $smtp->datasend("\n"); $smtp->datasend("test\n"); $smtp->dataend(); $smtp->quit;

      Thanks for the explanation of the " " and ' ', that makes a lot more sense now.

      Don't seem to be able to get the code above to work though, it just hangs for about 5 seconds, then I get this message 'Can't call method "mail" on an undefined value at line 6'

      What does the $smtp->mail(''); line actually do then? Compared to the $smtp->to('');?

        I tested it before I post, as I always do. I believe the problem is that you didn't replace the email server name and email address with real ones.

        The error you got basically means that $smtp is undef. I believe that is because you didn't change my dummy server name to your real one, so Perl can not establish the SMTP connection.
        Check out the docs for Net::SMTP and RFC 821 for the full answer but basically. $smtp->mail(''); is
        and $smtp->to(''); is....well
Re: Strange Situation with Net::SMTP
by nathan_j (Initiate) on Mar 26, 2003 at 05:56 UTC
    It would seem that you've my()ed $smtp twice. That's not good :-) Are you using warnings? (Try : "use warnings;", or call perl with "-w")
Re: Strange Situation with Net::SMTP
by Anonymous Monk on Mar 27, 2003 at 20:53 UTC
    I understand what you are saying.

    I can't tell you where (one of the perldeltas?) but I've read that Perl has to jump through some mighty hoops to make the Module->function() syntax work, and sometimes guesses wrong.

    new Net::SMTP $smtp_server or die "Failed:$1";
    It may not fix it, but it should be educational. To really force the matter, try:

    Net::SMTP::new('Net::SMTP', $smtp_server);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://245657]
Approved by Tanalis
Front-paged by dash2
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2020-05-30 16:19 GMT
Find Nodes?
    Voting Booth?
    If programming languages were movie genres, Perl would be:

    Results (173 votes). Check out past polls.