Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overwrite a function used in a module-method?

#!/usr/bin/env perl
use warnings;
use 5.012;
use utf8;

use WWW::Mechanize::Cached;
use Some::Module qw(some_method);

my $url = '...';
my $result = some_method( $url );

The some_method() uses itself get() form LWP::Simple.
How could I overwrite the get() with my my_get() in this script?

sub my_get {
    my $url;
    my $mech = WWW::Mechanize::Cached->new();
    $mech->get( $url );
    my $content = $mech->content( format => 'text' );
    return $content;
}
like image 893
sid_com Avatar asked Mar 06 '12 11:03

sid_com


People also ask

How do you overwrite a function in Python?

In Python method overriding occurs by simply defining in the child class a method with the same name of a method in the parent class. When you define a method in the object you make this latter able to satisfy that method call, so the implementations of its ancestors do not come in play.

Can you override built in functions in Python?

Builtin functions All Python applications rely on them, and usually don't expect that these functions are overriden. In practice, it is very easy to override them.


1 Answers

sub WWW::Mechanize::Cached::get {
    # your code
}

OR, if the get method is actually, as you imply in the question, is inherited from LWP::Simple -

sub LWP::Simple::get {
    # your code
}
like image 66
DVK Avatar answered Oct 11 '22 10:10

DVK