Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2022-09-28 10:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (124 votes). Check out past polls.

    Notices?