新しいblogに移行しました

新ブログ "All Yout Bugs Are Belong To Ass" に移行しました!

2009-04-21

[Perl]MooseX::Alienで、なんでもMoose

MooseX::Alienを使うと、非MooseなモジュールもMoose化できるらしい。

package MooseX::Safarize;
use Data::Dumper;
use Moose;
extends 'WWW::Mechanize';
with 'MooseX::Alien';

sub BUILD {
my $self = shift;
my @arg = @_;
$self->agent_alias( 'Mac Safari' );
}

before get => sub {
my ( $self, $uri ) = @_;
warn "GET $uri";
};

after get => sub {
my $self = shift;
warn "Status: ". $self->status. " ". length( $self->content ). " bytes DLed.";
};

no Moose;

my $c = MooseX::Safarize->new( timeout => 2 );
print Dumper $c->agent;
$c->get( 'http://www.cpan.org/' );


結果。

$VAR1 = 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/85 (KHTML, like Gecko) Safari/85';
GET http://www.cpan.org/ at ./safarize.pl line 15.
Status: 200 5810 bytes DLed. at ./safarize.pl line 20.

0 件のコメント: