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

In reply to Re: Structure of a custom class (using Apache::Session) by Akhasha
in thread Structure of a custom class (using Apache::Session) by jeyroz

Title:
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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.