Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Hello Fine Monks,

References are giving me a big headache. Every time I think I have them figured out, Perl gently points out that I don't.

So here is the basic question: how do I access a hash that is created in one package and needed in another? It seems like that should be a simple question with a reasonably simple answer, but I keep banging my head up against a wall trying to get it to work. Below I will put trimmed down versions of my packages to illustrate my question more fully.

Package 1:

package Bio::DB::Das::Chado; use strict; use Bio::Root::Root; use vars qw(@ISA); @ISA = qw(Bio::Root::Root); sub new { my $class = shift; my $self = $class->SUPER::new(@_); # do some work here to extract a DBI $hashref my $cvterm_id; # this scalar is going to be a hashref while (my $hashref = $sth->fetchrow_hashref) { $$cvterm_id{$$hashref{termname}} = $$hashref{cvterm_id}; } return bless {dbh => $dbh, cvterm_id => $cvterm_id}, ref $self ||$self; } sub segment { my $self = shift; my ($name,$start,$end,$class,$version) = $self->_rearrange([qw(NAME START END CLASS VERSION)],@_); # _rearrange is in Bio::Root::Root; I'm allowed to use it # lets the Segment class handle all the lifting. return $self->Bio::DB::Das::Chado::Segment->new($name,$self,$start,$ +end); } 1;
Package 2:
package Bio::DB::Das::Chado::Segment; use strict; use Bio::Root::Root; use vars '@ISA'; @ISA = qw(Bio::Root::Root); sub new { my $self = shift; my ($name,$dbadaptor,$start,$end) = @_; #$dbadaptor is 'Bio::DB::Das::Chado' # now do all kinds of work validating and obtaining info my $cvterm_id = $dbadaptor->{cvterm_id}; # so $cvterm_id should be a reference to the same # hash referenced in the first package, right? return bless {dbadaptor => $dbadaptor, start => $start, end => $end, length => $length, srcfeature_id => $srcfeature_id, cvterm_id => $cvterm_id, name => $name }, ref $self ||$self; } sub features { my $self = shift; # do lots more stuff, mostly to build a sql query my %termhash = %{$self->{cvterm_id}}; # shouldn't this be a reference to the hash ref in this #package's constructor, that in turn is a ref to the # hash ref in the first package? my @keys = grep(/\sgene/i , keys %termhash ); # this fails because %termhash isn't a hash. return; } 1;
Any pointers would be appreciated. Thanks for looking through this code; I hope I usefully trimmed it down.

Project coordinator of the Generic Model Organism Database Project

In reply to Hash references moving between modules by scain

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (18)
    As of 2014-09-16 17:31 GMT
    Find Nodes?
      Voting Booth?

      How do you remember the number of days in each month?

      Results (38 votes), past polls