Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Structure of a custom class (using Apache::Session)

by Akhasha (Scribe)
on Jun 16, 2005 at 01:19 UTC ( [id://467128]=note: print w/replies, xml ) Need Help??


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);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://467128]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (1)
As of 2024-04-24 13:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found