Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: Help to override Spreadsheet/ParseXLSX.pm module

by bliako (Monsignor)
on Apr 03, 2021 at 19:43 UTC ( [id://11130784]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Help to override Spreadsheet/ParseXLSX.pm module
in thread Help to override Spreadsheet/ParseXLSX.pm module

I guess this is the hybrid "monkeyclassing"! Was package ParseXLSX; at the 1st line of both original and patched files? If yes then you define a class (package) and then you redefine that class in the 2nd CE_ file. I have no idea what's meant to happen in this case. Obviously it did work for you and in your circumstances but I do not know how robust this is. For example, reversing the order of use statements would have different effect.

Here is a tiny example of subclassing in Perl. You subclass class P1 by creating a new class in a new file P2.pm . Consider this:

# file P1.pm package P1; sub new { my $class = shift; return bless {} => $class } sub A1 { print "i am A1 in P1\n"; } sub A2 { print "i am A2 in P1\n"; } 1;
# file P2.pm package P2; use parent 'P1'; sub new { my $class = shift; return bless {} => $class } sub A1 { print "i am A1 in >>>P2<<< (i have overwritten previous A1)\n +"; } # A2 and everything else are inherited from class P1 1;
# file test.pl # run: perl -I. test.pl use P2; # and not P1 my $P2obj = P2->new(); $P2obj->A1(); # this is overwritten $P2obj->A2(); # this is inherited verbatim from parent P1

The above demonstrates how P2 inherits all methods from P1 and then overwrites A1() but all other still hold as they were defined in P1.

How does that translate to your case? P1 is the original class/package. And P2.pm is the new file and package you created essentially overwriting 2 methods. The caveat here is that ALL your scripts which mention use P1; now should be modified to use P2;. Well OK, unless you don't have this power.

bw, bliako

Replies are listed 'Best First'.
Re^4: Help to override Spreadsheet/ParseXLSX.pm module
by boleary (Scribe) on Apr 06, 2021 at 11:31 UTC

    Yes, the first line in both files was package ParseXLSX
    I can see how Rolf's monkey-patching looks a bit safer than the way I did it so I will give that a go

    Thanks for taking the time to write up this quick tutorial! It was pretty helpful to me
    Now I think I can also see why sub-classing will not work in my application because I don't get to create the $P2obj on my own...
    it gets created in the Spreadsheet::ReadData method.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11130784]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 04:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found