File Coverage

blib/lib/WWW/Agent/Zombie.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package WWW::Agent::Zombie;
2              
3 1     1   167791 use strict;
  1         3  
  1         40  
4 1     1   6 use warnings;
  1         2  
  1         34  
5              
6 1     1   6 use Data::Dumper;
  1         1  
  1         44  
7 1     1   1132 use POE;
  1         59246  
  1         7  
8              
9             =pod
10              
11             =head1 NAME
12              
13             WWW::Agent::Zombie - Walking through websites like a zombie
14              
15             =head1 SYNOPSIS
16              
17             use WWW::Agent::Zombie;
18             my $z = new WWW::Agent::Zombie ();
19             $z->run (q{...
20             # some WeeZL here
21             });
22              
23             =head1 DESCRIPTION
24              
25             This package provides a way to let zombies stalk the earth. Seriously,
26             a plan (written in WeeZL, a simple text language, see
27             L) controls a L object.
28              
29             =head1 INTERFACE
30              
31             =head2 Constructor
32              
33             The constructor expects a hash with the following key/value pairs:
34              
35             =over
36              
37             =item C (hash reference, optional):
38              
39             In a WeeZL script you can refer to functions which you can provide
40             here. The keys are the names of the functions as the can appear in
41             the WeeZL script, as values you have to pass in subroutine references.
42              
43             When such a subroutine is invoked, it will get the current context as
44             parameter. See L for details.
45              
46             Example:
47              
48             my $zombie = new WWW::Agent::Zombie (functions => {
49             'test' => sub {
50             warn "here I am";
51             }
52             'test2' => sub {
53             warn "and again";
54             },
55             });
56              
57             =item C (string, percentation number, optional)
58              
59             In WeeZL scripts you can ask the agent to pause for a time interval.
60             If you specify there C<~ 4 secs> (wait for approximately 5 seconds),
61             then the time dither factor controls, what I means.
62              
63             In case of C<20%>, the actual waiting time will randomly range from 4
64             to 6 seconds.
65              
66             The default is C<10%>.
67              
68             =back
69              
70             =cut
71              
72             sub new {
73             my $class = shift;
74             my %options = @_;
75             my $self = bless {}, $class;
76              
77             $self->{functions} = delete $options{functions} || {};
78             $self->{time_dither} = delete $options{time_dither} || '10%';
79             # die "unsupported dithering spec '".$self->{time_dither}."'" unless $self->{time_dither} =~ /^(\d+)\%$/;
80             # $self->{time_dither} = $1;
81             $self->{ua} = delete $options{ua};
82              
83 1     1   176592 use WWW::Agent;
  1         4  
  1         43  
84             # use WWW::Agent::Plugins::LWP;
85 1     1   787 use WWW::Agent::Plugins::Focus;
  0            
  0            
86             # use WWW::Agent::Plugins::History;
87             use WWW::Agent::Plugins::Director;
88             new WWW::Agent (ua => $self->{ua},
89             plugins => [
90             # new WWW::Agent::Plugins::LWP,
91             new WWW::Agent::Plugins::Focus,
92             # new WWW::Agent::Plugins::History (length => 10),
93             new WWW::Agent::Plugins::Director (time_dither => $self->{time_dither},
94             functions => $self->{functions},
95             exception => sub { $self->{exception} = shift; }),
96             ]);
97             return $self;
98             }
99              
100             =pod
101              
102             =head2 Methods
103              
104             =over
105              
106             =item C
107              
108             This method expects a string with a script written in WeeZL. If that
109             is missing, the default
110              
111             die "no plan to run"
112              
113             will be used. Once this executes, obviously we return with an exception.
114              
115             The method will not return until the WeeZL script has terminated. Any
116             infinite loop there will be exactly that. If the WeeZL script contains
117             execptions, these will be caught and re-raised into your application.
118              
119             Example:
120              
121             $zombie->run (q{
122             goto http://www.example.org/
123             wait ~ 15 secs
124             goto http://www.example.org/login.php
125             });
126              
127             =cut
128              
129             sub run {
130             my $self = shift;
131             my $plan = shift || q|die "no plan to run"|;
132              
133             POE::Kernel->post ( 'agent', 'director_execute', 'zombie', $plan );
134             POE::Kernel->run ();
135              
136             die $self->{exception} if $self->{exception}; # if POE ended with an exception, we raise it here
137             }
138              
139             =pod
140              
141             =back
142              
143             =head1 AUTHOR
144              
145             Robert Barta, Erho@bigpond.net.auE
146              
147             =head1 COPYRIGHT AND LICENSE
148              
149             Copyright (C) 2005 by Robert Barta
150              
151             This library is free software; you can redistribute it and/or modify
152             it under the same terms as Perl itself, either Perl version 5.8.4 or,
153             at your option, any later version of Perl 5 you may have available.
154              
155             =cut
156              
157             our $VERSION = '0.04';
158             our $REVISION = '$Id: Zombie.pm,v 1.2 2005/03/19 05:08:17 rho Exp $';
159              
160             1;
161              
162             __END__