Hi monks,
I'm new to writing perl modules and am attempting to build and return a hash from within a .pm.
When i retrieve my hash all i get is its "scalar" value, not the hash itself.
Here's a cutdown version of my code:
the module:
package MeSH::KOL; use strict; use vars qw($VERSION); $VERSION = '0.01'; sub new { my ($class) = @_; my $self = {}; $self->{KOLS} = (); bless $self, $class; return $self; } # end-sub sub build_KOL { my($self) = @_; # for this example my %results = (); $results{1} = "blah1"; $results{2} = "blah2"; $self->{KOLS} = %results; } # end-sub sub get_all_KOLS { my ($self) = @_; return $self->{KOLS}; #my %hash = (); #$hash{1} = "blah1"; #$hash{2} = "blah2"; #return %hash; } # end-sub
the script:
#/usr/bin/perl use MeSH::KOL; my $kol_q = new MeSH::KOL; $kol_q->build_KOL; my (%kols) = $kol_q->get_all_KOLS; foreach my $auth_id (keys %kols) { print "$auth_id = $kols{$auth_id}\n"; } # end-foreach
This outputs 72/128 =
It works when i use the commented out hash in "get_all_KOLS"...
Can anyone tell me what i'm doing wrong?
Cheers,
Reagen

Update: Thanks guys... Also for the push to read perlref... doin that now :) .

In reply to Hashes in Perl Modules by rsiedl

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.