Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

G'day jms53,

To use Data::Dumper to display a hash, you should probably be using a reference to the hash in question. Consider this which uses a hash:

$ perl -Mstrict -Mwarnings -e ' use Data::Dumper; my %x = (a => 1); print Dumper %x; ' $VAR1 = 'a'; $VAR2 = 1;

Now compare the output using a reference to the hash:

$ perl -Mstrict -Mwarnings -e ' use Data::Dumper; my %x = (a => 1); print Dumper \%x; ' $VAR1 = { 'a' => 1 };

Using $date as a key seems ill-advised. Are there ever more than a single transaction on any given day? If so, you'll be overwriting all transactions for that day with the last one from the result set. Aim for unique keys: perhaps a combination of date and a transaction ID.

You're calling get_transactions() as MyDB->get_transactions(10000). I'll assume this is a class method with MyDB being the class. You could remove any ambiguity for Perl by writing MyDB::->get_transactions(10000); however, that's not your real problem here: the first argument to get_transactions() will be the class name and the second argument will be 10000, so your code for get_transactions() should look more like this:

sub get_transactions { my ($class, $transaction_id) = @_; ... }

Presumably, you're getting an empty result set because you don't have any transaction IDs called "MyDB". Adding a few basic debugging statements would have identified this issue for you rather quickly, e.g.

... my $transactions_id = shift; print "$transactions_id\n"; ... my $ar = $dbh->selectall_arrayref( $sql, undef, $transactions_id ) +; print Dumper $ar; ...

I'd also take a look at $dbh which appears out of the blue in get_transactions(). It might be worth passing that in as another argument so you know exactly what database handle you're using rather than relying on a correct global variable being in scope.

Finally, as a general rule-of-thumb, I'd always return a hashref, in preference to a hash, from a subroutine unless I had a very good reason not to. With a hashref, you're only returning a single scalar; a hash with the entire result set might contain hundreds, thousands or millions of scalars. And are you calling get_transactions() once or in a loop? [Use the same rule-of-thumb for returning an arrayref in preference to an array.]

-- Ken


In reply to Re: returning a hash from a module by kcott
in thread returning a hash from a module by jms53

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found