in reply to Structure of a custom class (using Apache::Session)
As for using a hash as a backing store but only using 1 key, its only a little wasteful if there are never more than one of these objects around, and its flexible when/if you ever need to add more fields/properties.
Its a little more efficient to make $self a blessed reference to an array.
I'm not sure about blessing a reference to your tied hash but you could try it. I tend to think that would be dangerous as the creation of your backing store is then out of your hands, or at least not where a reader might expect it to be.
If you're sure you'll never need more than one field in your class, you can bless a reference to a scalar. That scalar can in turn be a reference to your tied hash. It seems a little silly if there aren't many of these objects, but probably is a little more efficient than having a hash with one key.
#!/usr/bin/perl -w package Testing; sub new { my $class = shift; my $ref = shift; my $self = \$ref; bless $self, $class; } sub get { my $self = shift; ${$self}->{shift()}; } package main; my %h = (one => 1, two => 2); my $t = Testing->new(\%h); print $t->get($_)."\n" for qw(one two);
|
---|