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

  • SEARCH
  • Home
  • 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 6605081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:14:29+00:00 2026-05-25T19:14:29+00:00

This is the mock module I’m using: http://metacpan.org/pod/Test::MockModule How to mock sub a to

  • 0

This is the mock module I’m using:

http://metacpan.org/pod/Test::MockModule

How to mock sub a to sub b,

where sub b just does something else before call sub a?

sub b {
  #do something else
  a(@_);
}
  • 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-25T19:14:29+00:00Added an answer on May 25, 2026 at 7:14 pm

    You can grab the un-mocked method with can ( UNIVERSAL::can ). After that you can either goto it or just use the ampersand calling style to pass the same arguments. That’s what I did below.

     my $old_a = Package::To::Be::Mocked->can( 'a' );
     $pkg->mock( a => sub { 
          # do some stuff
          &$old_a;
     });
    

    This of course assumes that your sub isn’t AUTOLOAD or generated through AUTOLOAD without redefining can. (I learned years back that if you’re going to mess with AUTOLOAD, it’s probably best to do the work in can.)

    You could also create your own utility that does this automatically, by invading modifying the Test::MockModule‘s namespace.

    {   package Test::MockModule;
        sub modify {
            my ( $self, $name, $modfunc ) = @_;
            my $mock_class = $self->get_package();
            my $old_meth = $mock_class->can( $name );
            croak( "Method $name not defined for $mock_class!" ) unless $old_meth;
            return $self->mock( $name => $modfunc->( $old_meth ));
        }
    }
    

    And you could call it like so:

    $mock->modify( a => sub { 
        my $old_a = shift;
        return sub { 
            my ( $self ) = @_;
            # my stuff and I can mess with $self
            local $Carp::CarpLevel += 1;
            my @returns = &$old_a;
            # do stuff with returns
            return @returns;
        };
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a mock for the node-redis module and using Jasmine to test
Say I want to test this module: import osutils def check_ip6(xml): ib_output = osutils.call('iconfig
I'm trying to work out how this Mock (using the Moq framework) stuff all
I'm using mockito to test a legacy JAAS/LDAP login module. The javax.security.auth.callback.CallbackHandler interface defines
How do I mock the method GetValues() in System.Data.IDataReader? This method changes the array
I was wondering how I can mock the ClaimseReponse class in DotNetOpenAuth? This is
I recently found an article online that told me about this: RewriteRule ^mock-up/([^/]+)/([^/]+) /mock-up/index.php?page=$1&section=$2
We are not able to mock this class in RhinoMocks. public class Service {
We have just released a re-written(for the 3rd time) module for our proprietary system.
Can someone help me rewrite this regex to be non-exponential? I'm using perl to

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.