Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Hash of References in an Object

by pboin (Deacon)
on May 26, 2005 at 13:21 UTC ( #460669=perlquestion: print w/replies, xml ) Need Help??

pboin has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks.

I'm trying to stretch the head a bit and advance through the next plateau, but as you know, that's not always easy. I have a module (Product::Datafile) that creates an object. That datafile object in turn has ten or so objects of type (Cobol::Copybook).

My low-tech way, which seems valid was have a new entry in the hash containing a reference for each copybook. That's a legit thing to do I believe:

$self->{'cpy00'} = Cobol::Copybook->new( {filehandle => \*DATA, filter => '00'}); $self->{'cpy01'} = Cobol::Copybook->new( {filehandle => \*DATA, filter => '01'}); $self->{'cpy04'} = Cobol::Copybook->new( {filehandle => \*DATA, filter => '04'});

Now, striving to 'think right', I realized that I can't be Lazy and loop through these copy books 'cause they're not in a data structure like they should be. My 'loop' would have to be hard-coded, and edited whenever a new copybook comes online.

So, I want to have a hash keyed on name, with values being references to copybook objects. I know it's the old HofH trick but I'm on a bit of sensory overload with the extra dynamics of OO code and I just can't see clearly. Here's what I tried:

$self->{'copybooks'} = { 'cpy00' => Cobol::Copybook->new({filehandle => \*DATA, filter => '00 +'}), 'cpy01' => Cobol::Copybook->new({filehandle => \*DATA, filter => '01 +'}), 'cpy04' => Cobol::Copybook->new({filehandle => \*DATA, filter => '04 +'}) };
That looks right to my not-so-confident eye, but my pal Data::Dumper gives:
$VAR1 = { 'cpy04' => bless( { 'filter' => '04', 'fh' => \*Product::Datafile::DATA }, 'Cobol::Copybook' ), 'cpy01' => bless( { 'filter' => '01', 'fh' => $VAR1->{'cpy04'}{'fh'} }, 'Cobol::Copybook' ), 'cpy00' => bless( { 'filter' => '00', 'fh' => $VAR1->{'cpy04'}{'fh'} }, 'Cobol::Copybook' ) };

The references are all borken, and I *know* things aren't correct, espeically with the cross-references between '00', '01', and '04'. Seeing the bless() is kinda weird too, right?

PS: I'd appreciate better title suggestions. Please /msg me with suggestions if you have one.

Replies are listed 'Best First'.
Re: Hash of References in an Object
by dragonchild (Archbishop) on May 26, 2005 at 13:23 UTC
    You're getting confused with how Data::Dumper displays stuff. All three filehandles are identical, so it's pointing out that if you do something to one fh, you're doing it to the fh referenced in all three.

    Personally, given that your keys are "cpy##", I'd use an array of hashes.

    $self->{copybooks} = [ Cobol::Copybook->new({filehandle => \*DATA, filter => '00'}), Cobol::Copybook->new({filehandle => \*DATA, filter => '01'}), undef, undef, Cobol::Copybook->new({filehandle => \*DATA, filter => '04'}), ];

    • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
    • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"
Re: Hash of References in an Object
by Joost (Canon) on May 26, 2005 at 13:29 UTC
Re: Hash of References in an Object
by LanceDeeply (Chaplain) on May 26, 2005 at 14:36 UTC
    not what you asked for but... you may want to make your instantiation of copy books more flexible. perhaps add a method to Product::Datafile that takes in an array of filter codes
    sub createCopybooks { my $self = shift; my @filterCodes = @_; my %copyBooks; foreach my $filterCode ( @filterCodes ) { my $copyBook = Cobol::Copybook->new({filehandle = +> \*DATA, filter => $filterCode} ); my $copyBookName = 'cpy' . $filterCode; $copyBooks{$copyBookName} = $copyBook; } $self->{copybooks} = \%copyBooks; }
    then you can easily change how many copybooks you want like so:
    $productDatafile->createCopybooks('00', '01', '04', '42', '99');
    hth!
Re: Hash of References in an Object
by GoCool (Scribe) on May 26, 2005 at 19:37 UTC
    So basically your question is:
    Has converting from your first method of represnting the hash to the second method broken the references judging by the output from Data::Dumper. If so, then the answer is no. dragonchild is right on this one, the Data::Dumper module realises that filehandle of all your cpyXX objects reference the exact same DATA and so these cross-references in Data::Dumper look perfectly valid to me.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://460669]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2021-06-20 11:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (95 votes). Check out past polls.

    Notices?