Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^3: Help to override Spreadsheet/ module (monkey-patching)

by LanX (Sage)
on Apr 03, 2021 at 12:42 UTC ( #11130772=note: print w/replies, xml ) Need Help??

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

It's not clear to me how you did the "sub-classing", by inheriting methods or by importing or a do or ...?

Anyway, as I already said, I prefer monkey-patching over subclassing and you won't need to create another module/namespace for it.

Sub-classing requires that the author played by "the" book, and the OOP book is indeed a TIMTOWTDI library in Perl.

Monkey-patching OTOH is well defined and has no dependency to any OOP model.

So in our own interest consider

sub call_patched { local *Spreadsheet::ParseXLSX::new = \&patched_new; local *Spreadsheet::ParseXLSX::_parse_workbook = \&patched_parse_w +orkbook; # do whatever with Spreadsheet::ParseXLSX }

And all patches will disappear outside this function.

For completeness: Both ways may fail if there are any lexical variables closed over the patched subs, but even this can be solved with PadWalker, iff this really happens.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

  • Comment on Re^3: Help to override Spreadsheet/ module (monkey-patching)
  • Download Code

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

    Thanks Rolf! This is helpful and I will try it out...

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2021-10-28 09:26 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (96 votes). Check out past polls.