Hello HipDeep, and welcome to the Monastery!

I've also seen quite a few references to the concept that you should never access data structures directly, that you should always use a method. But I'm not sure that would apply directly in this case.

That is quite correct, and I don’t see why it wouldn’t apply in this case. Here is how I would refactor the modules:

package Monks::Data; use strict; use warnings; use parent 'Monks'; BEGIN { use Exporter(); our $VERSION = 20160808.00; our @ISA = qw(Exporter); } sub version { $Monks::Data::VERSION; } { my @fluffy; BEGIN { @fluffy = qw( Rabbits Minks Cats ); } sub is_fluffy { my $animal = shift; for (@fluffy) { return 'Yes' if $animal eq $_; } return 'No'; } } { my %textures; BEGIN { %textures = ( Rabbits => 'soft', Minks => 'supersoft', Cats => 'coarse', ); } sub get_texture { my $animal = shift; my $texture = 'unknown'; $texture = $textures{$animal} if exists $textures{$animal}; return $texture; } } 1;
package Monks; use strict; use warnings; use Monks::Data; BEGIN { use Exporter; our $VERSION = 0.01; our @ISA = qw(Exporter); } sub version { $Monks::VERSION } sub new { my $class = shift; my $self = { @_ }; bless $self, $class; return $self; }; sub check_fluffy { return Monks::Data::is_fluffy($_[1]); } sub find_fur_texture { return Monks::Data::get_texture($_[1]); } 1;

The value of this indirection is that it allows the implementation of package Monk::Data to be changed at a later date without impacting the code of its client(s). This is a general OO rule: keep the implementation separate from the public interface, and keep it private.

There are also some situations where I need to access the same type of data that is hard-coded into the main module. The common answer for that seems to be to copy that data into the object when it's created, but that seems inefficient in my case because I have some pretty large structures defined to handle a variety of cases where any individual object is only going to need a sliver of that data.

I don’t understand this. Can you elaborate? — preferably with example code.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: OO manner of accessing static variables in a subclass? by Athanasius
in thread OO manner of accessing static variables in a subclass? by HipDeep

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.