Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reclassify Perl object

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!

like image 311
Daniel Standage Avatar asked Nov 28 '22 23:11

Daniel Standage


1 Answers

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.

like image 64
friedo Avatar answered Dec 20 '22 04:12

friedo