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

Hi monks,
I have a module (using MIME::Lite) that has a function to send e-mails:
sub sendmail { my $param = shift; print $param->{'to'}; #my $to=$param->{'to'}; #print 'toto je 2 : '. $to; my $email= MIME::Lite->new( From=>"$param->{'from'}", To=>"$param->{'to'}", Subject=>"$param->{'subject'}", Data=>"$param->{'data'}" ); $email->send; }
I am trying to call it like this from my scripts:
$mail->sendmail({to=>'koszta@seznam.cz', from=>'admin@server.com', dat +a=>'hi from server', subject=>'msg body'});
What am I missing - the hash doesn't get passed and my $param->{'something'} are just empty strings.... I followed this but apparently it doesnt work... thanks

Replies are listed 'Best First'.
Re: passing a hash to module function
by wind (Priest) on Feb 15, 2011 at 19:35 UTC
    Your problem lies in the fact that you're calling sendmail as a method.
    $mail->sendmail({to=>'koszta@seznam.cz', from=>'admin@server.com', dat +a=>'hi from server', + subject=>'msg body'});
    Either, call it as a function if that is what's appropriate
    sendmail({to=>'koszta@seznam.cz', from=>'admin@server.com', data=>'hi +from server', + subject=>'msg body'});
    Or remember to pull the object reference first.
    sub sendmail { my $self = shift; my $param = shift;
      Thank you, I am just getting my way around in perl (comming over from php is tough but definitelly worth it)... I love CPAN it has anything... But I get stuck at issues like this. Well I am gonna learn it!
Re: passing a hash to module function
by Marshall (Canon) on Feb 15, 2011 at 18:56 UTC
    Take out the extra curly braces..
    $mail->sendmail(to=>'koszta@seznam.cz', from=>'admin@server.com', data=>'hi from server', subject=>'msg body');

      That won't help, for two reasons. One is that the code accesses $param as a hash reference. The more important reason is that the function gets called as a method but doesn't deal with the invocant.

        ok so how do I fix it? I dont know how to pass it properly... I am okey just passing it somehow. I want to have a hash passed to that funtion
Re: passing a hash to module function
by bprice (Acolyte) on Feb 17, 2011 at 10:04 UTC

    References are good. Here's a working version - you should be able to figure it out from this:

    #!/usr/bin/perl use strict; use warnings; use MIME::Lite; sendmail(\{to=>'koszta@seznam.cz', from=>'admin@server.com', data=>'hi + from server', subject=>'msg body'}); sub sendmail { my $param = shift; my $email= MIME::Lite->new( From=>"$$param->{'from'}", To=>"$$param->{'to'}", Subject=>"$$param->{'subject'}", Data=>"$$param->{'data'}" ); # debug output print "I would now send from $$param->{'from'} to $$param->{'t +o'}:\n"; print "Subject: $$param->{'subject'}\n\n"; print "$$param->{'data'}\n"; # disable sending as I'm only testing :-) # $email->send; }