PetaMem has asked for the wisdom of the Perl Monks concerning the following question:
I write you this question with happy feelings on one side, as I was able to solve some (for me) very hard problems of a project Iīm working on. On the other side, now Iīm stuck in the "technically elegant" mania and the Perl characteristics donīt seem to allow me things I would like to do.
I have 3 object classes. Letīs call them Dictionary, Entry and Parser.
Dictionary has the following constructor:
Entry has the following constructor (as of now):sub new { my $pkg = shift; my %dictionary; my $self = \%dictionary; bless $self, $pkg; }
The constructor of Parser is irrelevant - I just donīt know it Parse::RecDescent creates it.sub new { my $pkg = shift; my $self = \\@_; bless $self, $pkg; }
Now Dictionary shall be a hash of Entries, where each Entry is a List of something. A Parser object creates (allocates) this list and returns a reference to it. Thus my first shot at Entry was:
However - I wasnīt able to "pass this reference" returned from Parser to the reference member of the Entry object. Someone here at the Chatterbox told me, that this indeed isnīt possible and that I have to use a reference to a reference as element of the object. Therefore the \\@_.sub new { my $pkg = shift; my $self = \@_; bless $self, $pkg; }
This seems contraintuitive to me. But wait! Thereīs more:
So Now I have those reference to a reference Entry objects and adjusted my code to cope with it. But now the problems keep in coming. I wrote a simple method in the Entry Class just for debugging purposes, that shall print the contents of the list Entry points to:
This works nice, as long as I have really a reference to a reference at hand:sub eprint { my $self = shift; my $key = shift; print "KEY: $key\n"; foreach my $show (@{$self}) { print "element: $show\n"; } }
If I donīt, hell breaks loose:$self->{$key} = $$entry; # anchor reference in dictionary $entry->eprint($key); # just for debug purpose (WORKS)
Now what would be ideal? Ideal would be, If I wouldnīt need to have a reference to a reference stored in Entry but just reference to list. And if I could use Entry-methods consistently through my code.# remeber: value of the hash-element with # key $k1 is an Entry object my $ref1 = $dictionary{$l1}->{$k1}; # works foreach my $meaning (@{$ref1}) { &message("\t$meaning\n"); } $ref->eprint($k1); # no chance :-(
Any help - as always my patient brothers - is greatly apreciated.
Ciao
Rico
BTW: Nodes read before writing this Node: 81683 76071 51815 (could here be a hidden answer?) 21775 20664 19121 and many others...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(tye)Re: Reference as the only Object element
by tye (Sage) on Nov 02, 2001 at 21:12 UTC | |
by PetaMem (Priest) on Nov 02, 2001 at 21:42 UTC | |
by dragonchild (Archbishop) on Nov 02, 2001 at 21:58 UTC | |
by tye (Sage) on Nov 02, 2001 at 23:10 UTC | |
by dragonchild (Archbishop) on Nov 02, 2001 at 23:22 UTC | |
|
Re: Reference as the only Object element
by dragonchild (Archbishop) on Nov 02, 2001 at 21:40 UTC |