2012-03-06 92 views
7
#!/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); 

some_method()本身使用get()形式LWP::Simple
我該如何在腳本中用我的my_get()覆蓋get()如何覆蓋模塊方法中使用的函數?

sub my_get { 
    my $url; 
    my $mech = WWW::Mechanize::Cached->new(); 
    $mech->get($url); 
    my $content = $mech->content(format => 'text'); 
    return $content; 
} 

回答

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

,或者,如果get方法其實,當你在問題中暗示,從LWP繼承::簡單 -

sub LWP::Simple::get { 
    # your code 
} 
+1

確保裝入要重寫模塊在你這樣做之前,你可能需要一個'不警告'重新定義''。我在_Mastering Perl_中廣泛地討論了這個問題。關於這個問題有一整章。 :) – 2012-03-06 15:16:29