Just a couple of other points.

Your statement handle would benefit from placeholders, and forming a closure for the prepared handle would allow the prepare call to only be done once:

# after $dbh is obtained { my $sth = $dbh->prepare "select hall_name, upload_date, uploaded_by, photo_id from hall_details where hall_id=? limit 1"; sub details_from_id { my $id = shift; $sth->execute $id; $sth->fetchrow_hashref; } # mod some error checking }
With that, you get the the advantages of global variables without some of the headaches. The DBI handle could itself be encapsulated this way. Is hall_id the primary key? If so the limit clause can be omitted.

The other point is that your returned hash is a level too deep. The caller already knows $hall_id - it was passed as an argument - and will just need to extract the lone value from your return. With that, you can just call details_from_id directly.

my @hall_ids = 1..20; my %details; @details{@hall_ids} = map { details_from_id $_ } @hall_ids;

After Compline,
Zaxo


In reply to Re: Making hashes from arrays by Zaxo
in thread Making hashes from arrays by CodeJunkie

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.