So after reading perltoot, I decided to have a go at creating some oo-perl code. I had a project in mind and started designing the various objects I would use and grouping them into various modules. I eventually decided that I wanted my constructor to be inherited by a subclass from its superclass. But I've run into trouble there. I'm using a slightly modified form of the AUTOLOAD for data accessors example from perltoot. In my first module Some/Package.pm I have:
package Some::Package; use strict; use warnings; our $VERSION = '0.1'; our $AUTOLOAD; my %fields = ( name => undef, ); sub new { my $proto = shift; my $class = ref($proto) || $proto; my %args = @_; my $self = { _permitted => \%fields, %fields, }; bless($self, $class); foreach my $arg (keys(%args)) { $self->$arg($args{$arg}); } sub AUTOLOAD { my $self = shift; my $type = ref($self) or croak "$self is not an object"; my $name = $AUTOLOAD; $name =~ s/.*://; unless(exists $self->{_permitted}->{$name} ) { croak "Can't access `$name' field in class $type"; } if (@_) { return $self->{$name} = shift; } else { return $self->{$name}; } } 1;
In the module that defines the subclass, Some/Package/Sub.pm, I have the following:
package Some::Package::Sub; use strict; use warnings; our $VERSION = '0.1'; our $AUTOLOAD; my %fields = ( name => undef, needthis => undef, ); 1;
Finally, my test script looks something like this:
use strict; use warnings; use Some::Package; use Some::Package::Sub; my $object = Some::Package::Sub->new( name => 'Test', needthis => 'Value', }
Now, it seems that Some::Package::Sub inherits the constructor and AUTOLOAD methds just fine, but that the inherited constructor, which makes use of a reference to the file scoped lexical %fields, refers to the hash in Some/Package.pm even though I'm calling Some::Package::Sub->new(). This means that the data accessor for the needthis attribute is not available in Some::Package::Sub objects. How do I solve this?

In reply to Inheritence of a Constructor that uses a file-scoped lexical? by Orsmo

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.