I'm working with a few Perl packages, we'll call them Some::Parser
and Some::Data
. A Some::Parser
object has methods to return objects of type Some::Data
. I have written a class that extends the Some::Data
class, let's call it My::Data
. Objects of class My::Data
are really just objects of class Some::Data
, but with additional methods that make it easier to work with.
My problem is that I want to continue to use the Some::Parser
class to do the hard work of parsing the data. As I said earlier, Some::Parser
objects give me Some::Data
objects. Once I have a Some::Data
object in hand, is there any way to reclassify it as a My::Data
object? How would I do this?
I'm totally willing to change my approach, assuming someone can suggest a better way of doing what I want to do, but writing my own parser is not something I'm interested in doing!
This smells like a bit of a kludge. It might be time to rethink your strategy. For example, maybe you should write My::Parser
which returns My::Data
objects.
But if you don't want to do that, you can manually use bless
to change an object's class:
my $obj = Some::Data->new;
bless $obj, 'My::Data';
See bless in perldoc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With