This is an embarassingly simple question, but its after 5 and my brain is dead. Google and the perl objects book are no help...

Today I refactored and abstracted 3 similar modules I had into one parent class and 3 subclassed. It cut down about half the code. Woo hoo.

Then I realized a shortcoming of my solution. Each of the subclasses has its own version of a certain variable.
package abstracted; our $name = 'parent'; package abstracted::versionA; our $name = 'vA'; package abstracted::versionB; our $name = 'vB';
A shared method moved into the parent class accesses $name. Naturally, it keeps pulling $abstracted::name , while i want $abstracted::versionA::name .

There are 2 ways I can think of solving this:
1. sub new { $self->{'__name'} = \$name }
2. my $name = eval( '\$' . ( ref $self ) . '::name' )
solution #1 is probably better, but i'm trying to cut down on vars i'm tossing in $self -- i'm just tossing too much in there to get things done.

solution #2 is a dirty hack , and probably wildly ineffient - but i only have to modify my code on 1 line ( vs setting the var in 3 sep. constructors )

does anyone have a suggestion or can share an idiomatic way to accomplish this?

In reply to Accessing class/subclass variables by nmerriweather

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.