I have two classes: the base class, Foo :: Base and the derived class, Foo::Base::Sub . I want Foo::Base::Sub do some type and data validation on a constructor argument - a hash - before blessing it. I tried to override the constructor Foo::Base->new by doing checks and then calling Foo::Base->new (since the code will be exactly the same):
package Foo::Base::Sub; sub new { ...check argument type and data... Foo::Base->new(%my_hash) }
The problem is that by invoking the Foo::Base construct, the hash will now be blessed as a Foo :: Base object, not a Foo :: Base :: Sub object. The obvious solution is to simply put the code from Foo::Base::new into Foo::Base::Sub::new , but then I repeat the code. Another thing is that Foo :: Base is not mine - so I would not want to change it after the module downloaded or deployed it unnecessarily.
It seems to me that this problem should have arisen earlier, and therefore there should be a canonical solution. Moreover, it does affect the type of enforcement, which is usually not a Perl issue.
So, is there a simple modification, or am I mistaken about this?
source share