I'm porting a small Java application to Perl, using Class::Std. Now I have a class with a constructor that calls the superclass constructor with certain required fields filled in. An example in Perl might be:
package myclass; use Carp qw(croak); sub new { my ($class, $value) = @_; croak "required value missing" if @_ == 1 or !$value; return bless \$value, $class; } package mysubclass; use base qw(myclass); sub new { my ($class) = @_; return bless $class->SUPER::new(23), $class; }
Now in Class::Std, superclass "constructors" (ie BUILD and START) are called automatically. However, there doesn't seem to be a way to set or override the arguments for a superclass in a subclass. I'd like to do something like this:
package myclass; use Class::Std; my %value :ATTR( :init_arg<value> ); package mysubclass; use base qw(myclass); sub BUILD { my ($self, $ident, $args) = @_; $args->{value} = 23; }
This doesn't work. Even though Class::Std calls BUILD(), then initialises the object from the constructor arguments, and then calls START(), the third argument passed to each BUILD/START method is specific to that method - its discarded afterwards. There doesn't seem to be any way to influence the arguments in this way. Doing this:
mysubclass->new;
gives:
Missing initializer label for myclass: 'value'. Fatal error in constructor call
Anyone got any ideas before I open a ticket in RT?

Cheers,
Rob.


In reply to Overriding arguments in Class::Std superclass constructors by fce2

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.