Sibling monks, I humbly solicit your advice. I often find myself wanting to populate a hash, not with plain values, but with the results of calculations or functions. For example, a lot of the modules I'm using (Mail::Sendmail, HTML::Template) seem to accept data in hash format. But doing it directly, by placing the function inside the hash, doesn't seem to work. I've resorted to assigning the result of each calculation / function to its own scalar, and then putting the scalars into the hash. But this doesn't feel like the best solution - it's two steps where I only want one, and it uses memory unproductively.

Here's an example from what I'm working on now (I'm generating an email to a customer - the info for the email is pulled out of some db tables and put into hashrefs all called $ref_something) (n.b. the "Message" is all on one line in my script - I've broken it up here just to make it easier to read):
use Mail::Sendmail; my $Date = substr(gmtime $ref_event->{'EventStartDateTime'},0,10); my $SecID = int rand 16777216; my %m; # what I'd like to do except it doesn't work: %m = ( To => $ref_provider->{'ProviderEmail'}, From => 'management@trainingboard.co.uk', Subject => 'COURSE AVAILABILITY INQUIRY', Message => "We have received an inquiry about availability on the following course run by you:\n\n Course Title:\t\t\t $ref_course->{'CourseTitle'}\n \nCourse Date:\t\t\t$Date\n\n Number of Places Requested:\t\t $ref_booking->{'NumberOfAttendees'}\n\n Please confirm within 24 hours whether sufficient places are available (and if not, what alternatives there may be) by visiting the following site: http://www.trainingboard.co.uk/dmp/ $ref_provider->{'ProviderID'}/booking.pl? Action=Confirm Availability&BookingID= $ref->{'BookingID'}&SecID=$SecID", ); # what I am doing at present but hope to improve on: my $To = $ref_provider->{'ProviderEmail'}; my $Message = "We have received an inquiry about availability on the following course run by you:\n\n Course Title:\t\t\t $ref_course->{'CourseTitle'}\n \nCourse Date:\t\t\t$Date\n\n Number of Places Requested:\t\t $ref_booking->{'NumberOfAttendees'}\n\n Please confirm within 24 hours whether sufficient places are available (and if not, what alternatives there may be) by visiting the following site: http://www.trainingboard.co.uk/dmp/ $ref_provider->{'ProviderID'}/booking.pl? Action=Confirm Availability&BookingID= $ref->{'BookingID'}&SecID=$SecID"; %m = ( To => $To, From => 'management@trainingboard.co.uk', Subject => 'COURSE AVAILABILITY INQUIRY', Message => $Message, );
I'd be most grateful if anybody can tell me a neater way to do it. I feel this is something I ought to know - if so, I apologise for my ignorance!

§ George Sherston

In reply to Passing functions etc into hashes by George_Sherston

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.