Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Passing variables to function's hash.

by FrankRizzo (Acolyte)
on Jan 18, 2005 at 22:00 UTC ( [id://423196]=perlquestion: print w/replies, xml ) Need Help??

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

Guys, I'm trying to use Mail::Sender, which expects to get a hash containing the parameters. Every example that I see uses a constant value I.E. 'To => joe@mama.com'. I'm trying to pass in variables for these values, and get "eval errors" when I attempt to compile/run the script. Would someone PLEASE save my sanity, and show/explain the proper technique for passing variables to a function that expects to get them in a hash? Thanks!

Replies are listed 'Best First'.
Re: Passing variables to function's hash.
by saskaqueer (Friar) on Jan 18, 2005 at 22:27 UTC

    Not exactly sure what you mean, but here's a couple of examples:

    # example #1 eval q{ my $msg = Mail::Sender->new( { from => 'you@example.com', to => 'me@example.net' } ); } or die("mail error: $Mail::Sender::Error"); # example #2 eval q{ my ($from, $to) = ('you@example.com', 'me@example.net'); my $msg = Mail::Sender->new( { from => $from, to => $to } ); } or die("mail error: $Mail::Sender::Error"); # example #3 eval q{ my %vars = ( from => 'you@example.com', to => 'me@example.net' ); my $msg = Mail::Sender->new( \%vars ); } or die("mail error: $Mail::Sender::Error");
Re: Passing variables to function's hash.
by Tanktalus (Canon) on Jan 18, 2005 at 22:22 UTC

    It would be extremely useful if you gave us a snippet of what you were trying - it'd help when trying to show where you're heading in the wrong direction.

    some_func( To => 'joe@mama.com', Subject => $subject, );
Re: Passing variables to function's hash.
by Roy Johnson (Monsignor) on Jan 18, 2005 at 22:30 UTC
    Unless it's a prototyped function, a parameter list is a parameter LIST. If it's prototyped to take a hash, you should pass it a hash (something beginning with %). It wouldn't hurt to do that, anyway.

    But I see from the docs that Mail::Sender wants a hash ref. Put your list in braces or put a backslash in front of your %.

    If you include some actual, minimal code that demonstrates the problem you're having, we could run it ourselves and it would be easier to debug.


    Caution: Contents may have been coded under pressure.
      Sorry about that guys, thought it might be a generic enough question to not NEED a code sample.
      my $from_name = $mail_from; my $from_address = $display_name; my $to_address = $_; my $subject = 'Daily Report'; my $mime_type = 'TEXT'; my $message = "Your PDF report is attached."; # Create the initial text of the message my $mime_msg = MIME::Lite->new( From => $from_address, To => $to_address, Subject => $subject, Type => $mime_type, Data => $message ) or die "Error creating MIME body: $!\n"; my $filename = 'daily.pdf'; my $recommended_filename = 'daily.pdf'; # Attach the file $mime_msg->attach( Type => 'application/pdf', Path => $filename, Filename => $recommended_filename ) or die "Error attaching test file: $!\n";
      As you can tell from the code, most, if not ALL the of important info comes from variables, most of which come from a config file, or are retrieved from a database.

        I installed MIME::Lite, took your code above, put in the missing "use MIME::Lite;" at the top, and ran it with perl 5.8.5. No compilation errors, no die's were triggered. Is this the exact piece of code that, by itself, is triggering the problem you're having?

        Thanks,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://423196]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-25 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found