package One; $cache = {}; sub query { my $sql = "select first, last, phone, address from db..table where las +t like 'A%'"; data($sql); } sub data { my $sql = @_; my $dsn = "dbi:mysql:$database:$host:$port"; my $dbh = DBI->connect($dsn,$user,$pwd) || die "Could not connect: $DB +I::errstr\n"; my $query = $dbh->selectall_arrayref($sql, { Slice => {} }); foreach my $row (@$query) { $cache{$row->{phone}}{last} = $row->{last}; $cache{$row->{phone}}{first} = $row->{first}; $cache{$row->{phone}}{address} = $row->{address}; } bless $cache{$row->{phone}}; }

I want to take the cache from package One and load it to a hash in another (package Two) to avoid having to call package One repeatedly.

package Two; use One; $hash = &One::query(); %hash = %$hash;

What I have doesn't work. Since there is no return statement in subroutine One::data, the last evaluation will be returned in One::query. In this case, is it the WHOLE cache? I am trying to get the WHOLE cache into the hash in package Two. All alternatives are helpful. I am trying to find all alternatives to this problem. Thanks in advance!!


In reply to Load cache by rocky13

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.