例えば、WWW::Mechanizeを継承したChildというクラスで、newした時点で必ずagentを'Mojimoji/1.0'にしたい場合。
### lib/Child.pm
package Child;
use warnings;
use strict;
use base qw/ WWW::Mechanize /;
use SUPER;
sub new {
my $class = shift;
my $self = $class->SUPER::new( @_ );
$self->agent( 'Mojimoji/1.0' );
$self;
}
1;
で、実際に使うときには
#!/usr/bin/perl
use lib qw( ./lib );
use Child;
use Data::Dumper;
my $c = Child->new;
print Dumper $c;
これを実行すると、
$VAR1 = bless( {
'headers' => {},
'forms' => [],
'page_stack' => [],
'requests_redirectable' => [
'GET',
'HEAD',
'POST'
],
'from' => undef,
'timeout' => 180,
'onerror' => sub { "DUMMY" },
'parse_head' => 1,
'_extracted_images' => 0,
'links' => [],
'max_redirect' => 7,
'quiet' => 0,
'images' => [],
'_extracted_links' => 0,
'stack_depth' => 8675309,
'protocols_forbidden' => undef,
'no_proxy' => [],
'onwarn' => sub { "DUMMY" },
'protocols_allowed' => undef,
'use_eval' => 1,
'autocheck' => 0,
'agent' => 'Mojimoji/1.0',
'def_headers' => undef,
'cookie_jar' => bless( {
'COOKIES' => {}
}, 'HTTP::Cookies' ),
'proxy' => {},
'max_size' => undef
}, 'Child' );
0 件のコメント:
コメントを投稿