Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1019391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:00:22+00:00 2026-05-16T11:00:22+00:00

I’m not thinking too clearly right now and possibly overlooking something simple. I’ve been

  • 0

I’m not thinking too clearly right now and possibly overlooking something simple. I’ve been thinking about this for a while and been searching, but can’t really think of any sensible search queries anymore that would lead me to what i seek.

In short, I’m wondering how to do module inheritance, in the way base.pm/parent.pm do it for object-oriented modules; only for Exporter-based modules.

A hypothetical example of what i mean:

Here’s our script. It originally loaded Foo.pm and called baz() from it, but baz() has a terrible bug (as we’ll soon see), so we’re using Local/Patched/Foo.pm now which should fix the bug. We’re doing this, because in this hypothetical case we cannot change Foo (it is a cpan module under active development, you see), and it is huge (seriously).

#!/usr/bin/perl

# use Foo qw( baz [... 100 more functions here ...] );
use Local::Patched::Foo qw( baz [... 100 more functions here ...] );
baz();

Here’s Foo.pm. As you can see, it exports baz(), which calls qux, which has a terrible bug, causing things to crash. We want to keep baz and the rest of Foo.pm though, without doing a ton of copy-paste, especially since they might change later on, due to Foo still being in development.

package Foo;
use parent 'Exporter';
our @EXPORT = qw( baz [... 100 more functions here ...] );
sub baz { qux(); }
sub qux { print 1/0; }            # !!!!!!!!!!!!!
[... 100 more functions here ...]
1;

Lastly, since Foo.pm is used in MANY places, we do not want to use Sub::Exporter, as that would mean copy-pasting a bandaid fix to all those many places. Instead we’re trying to create a new module that acts and looks like Foo.pm, and indeed loads 99% of its functionality still from Foo.pm and just replaces the ugly qux sub with a better one.

What follows is what such a thing would look like if Foo.pm was object-oriented:

package Local::Patched::Foo;
use parent 'Foo';
sub qux { print 2; }
1;

Now this obviously will not work in our current case, since parent.pm just doesn’t do this kinda thing.

Is there a clean and simple method to write Local/Patched/Foo.pm (using any applicable CPAN modules) in a way that would work, short of copying Foo.pm’s function namespace manually?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:00:23+00:00Added an answer on May 16, 2026 at 11:00 am

    Just adding in yet another way to monkey-patch Foo‘s qux function, this one without any manual typeglob manipulation.

    package Local::Patched::Foo;
    use Foo (); # load but import nothing
    
    sub Foo::qux {
        print "good qux";
    }
    

    This works because Perl’s packages are always mutable, and so long as the above code appears after loading Foo.pm, it will override the existing baz routine. You might also need no warnings 'redefine'; to silence any warnings.

    Then to use it:

    use Local::Patched::Foo;
    use Foo qw( baz );
    
    baz();  # calls the patched qux() routine
    

    You could do away with the two use lines by writing a custom import method in Local::Patched::Foo as follows:

    # in the Local::Patched::Foo package:
    
    sub import {
        return unless @_;             # return if no imports
        splice @_, 0, 1, 'Foo';       # change 'Local::Patched::Foo' to 'Foo'
        goto &{ Foo->can('import') }; # jump to Foo's import method
    }
    

    And then it is just:

    use Local::Patched::Foo qw( baz );
    
    baz();  # calls the patched qux()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.