line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::Lipsum; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
151478
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
60
|
|
4
|
2
|
|
|
2
|
|
6
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
67
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '1.001012'; # VERSION |
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
8
|
use Carp qw/croak/; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
82
|
|
9
|
2
|
|
|
2
|
|
624
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
29866
|
|
|
2
|
|
|
|
|
40
|
|
10
|
2
|
|
|
2
|
|
410
|
use Mojo::DOM; |
|
2
|
|
|
|
|
52047
|
|
|
2
|
|
|
|
|
52
|
|
11
|
2
|
|
|
2
|
|
456
|
use Moo; |
|
2
|
|
|
|
|
8995
|
|
|
2
|
|
|
|
|
9
|
|
12
|
|
|
|
|
|
|
use overload q|""| => sub { |
13
|
6
|
|
|
6
|
|
2969
|
my $self = shift; |
14
|
6
|
|
|
|
|
14
|
my $text = $self->generate; |
15
|
6
|
|
33
|
|
|
1525
|
return $text || '[Error: ' . $self->error . ']'; |
16
|
2
|
|
|
2
|
|
1296
|
}; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
15
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has what => ( is => 'rw', default => 'paras' ); |
19
|
|
|
|
|
|
|
has start => ( is => 'rw', default => 1 ); |
20
|
|
|
|
|
|
|
has amount => ( is => 'rw', default => 5 ); |
21
|
|
|
|
|
|
|
has html => ( is => 'rw', default => 0 ); |
22
|
|
|
|
|
|
|
has lipsum => ( is => 'rw', build_args => undef ); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
has error => ( is => 'rw', build_arg => undef ); |
25
|
|
|
|
|
|
|
has _ua => ( is => 'ro', build_arg => undef, default => sub { |
26
|
|
|
|
|
|
|
return LWP::UserAgent->new( timeout => 30, |
27
|
|
|
|
|
|
|
agent => 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:26.0) ' |
28
|
|
|
|
|
|
|
. 'Gecko/20100101 Firefox/26.0' |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
}); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub generate { |
33
|
7
|
|
|
7
|
1
|
1659
|
my $self = shift; |
34
|
7
|
|
|
|
|
17
|
$self->lipsum(undef); |
35
|
|
|
|
|
|
|
|
36
|
7
|
|
|
|
|
18
|
$self->_prep_args( @_ ); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# sometimes we fail to get decent Lipsum content, |
39
|
|
|
|
|
|
|
# so let's retry once in that case and bail out if we still fail |
40
|
7
|
|
|
|
|
8
|
my $did_already_retry = 0; |
41
|
7
|
|
|
|
|
9
|
my ( $res, $dom ); |
42
|
7
|
100
|
|
|
|
90
|
GET_LIPSUM: { |
43
|
7
|
|
|
|
|
7
|
$res = $self->_ua->post( 'http://lipsum.com/feed/html', { |
44
|
|
|
|
|
|
|
amount => $self->amount, |
45
|
|
|
|
|
|
|
what => $self->what, |
46
|
|
|
|
|
|
|
start => $self->start ? 1 : 0, |
47
|
|
|
|
|
|
|
generate => 'Generate Lorem Ipsum', |
48
|
|
|
|
|
|
|
}); |
49
|
|
|
|
|
|
|
|
50
|
7
|
50
|
|
|
|
2224581
|
return $self->_set_error( 'Network error: ' . $res->status_line ) |
51
|
|
|
|
|
|
|
unless $res->is_success; |
52
|
|
|
|
|
|
|
|
53
|
7
|
|
|
|
|
85
|
$dom = eval { |
54
|
7
|
|
|
|
|
170
|
Mojo::DOM->new( $res->decoded_content ) |
55
|
|
|
|
|
|
|
->find('#lipsum') |
56
|
|
|
|
|
|
|
->first |
57
|
|
|
|
|
|
|
->children; |
58
|
|
|
|
|
|
|
}; |
59
|
7
|
50
|
|
|
|
193345
|
@$ and return $self->_set_error("Parsing error: $@"); |
60
|
7
|
50
|
|
|
|
26
|
unless ( $dom ) { |
61
|
0
|
0
|
|
|
|
0
|
redo GET_LIPSUM unless $did_already_retry; |
62
|
0
|
|
|
|
|
0
|
$did_already_retry = 1; |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
0
|
return $self->_set_error( |
65
|
|
|
|
|
|
|
'Unknown error. HTML we got from Lipsum is: ' |
66
|
|
|
|
|
|
|
. $res->decoded_content |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
$self->html |
72
|
|
|
|
|
|
|
or return $self->lipsum( |
73
|
|
|
|
|
|
|
join "\n\n", map $_->all_text, |
74
|
7
|
100
|
|
11
|
|
79
|
$dom->grep(sub{ $_->tag eq 'p'})->each |
|
11
|
|
|
|
|
253
|
|
75
|
|
|
|
|
|
|
); |
76
|
|
|
|
|
|
|
|
77
|
2
|
|
|
|
|
10
|
my $html = join '', $dom->each; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# a little hackery to get rid of lipsum.com's invalid markup |
80
|
2
|
100
|
|
|
|
33295
|
$self->what eq 'lists' and $html =~ s{ | }{}gi; |
81
|
2
|
|
|
|
|
11425
|
$html =~ s/^\s+|\s+$//g; |
82
|
|
|
|
|
|
|
|
83
|
2
|
|
|
|
|
1137
|
return $self->lipsum( $html ); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub _set_error { |
87
|
0
|
|
|
0
|
|
0
|
my ( $self, $error ) = @_; |
88
|
0
|
|
|
|
|
0
|
$self->error( $error ); |
89
|
0
|
|
|
|
|
0
|
return; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub _prep_args { |
93
|
7
|
|
|
7
|
|
9
|
my $self = shift; |
94
|
7
|
|
|
|
|
15
|
my %args = @_; |
95
|
|
|
|
|
|
|
|
96
|
7
|
50
|
|
|
|
20
|
$self->start( $args{start} ) |
97
|
|
|
|
|
|
|
if exists $args{start}; |
98
|
|
|
|
|
|
|
|
99
|
7
|
50
|
|
|
|
16
|
$self->html( $args{html} ) |
100
|
|
|
|
|
|
|
if exists $args{html}; |
101
|
|
|
|
|
|
|
|
102
|
7
|
50
|
|
|
|
17
|
if ( defined $args{what} ) { |
103
|
0
|
0
|
|
|
|
0
|
croak q{Argument 'what' must be one of 'paras', 'words', 'bytes',} |
104
|
|
|
|
|
|
|
. q{ or 'lists'} |
105
|
|
|
|
|
|
|
unless $args{what} =~ /\A(paras|words|bytes|lists)\z/; |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
0
|
$self->what( $args{what} ); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
7
|
50
|
|
|
|
17
|
if ( defined $args{amount} ) { |
111
|
0
|
0
|
0
|
|
|
0
|
croak q{Argument 'amount' must contain a positive integer} |
112
|
|
|
|
|
|
|
unless $args{amount} and $args{amount} =~ /\A\d+\z/; |
113
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
0
|
$self->amount( $args{amount} ); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
7
|
|
|
|
|
17
|
return; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
q| |
121
|
|
|
|
|
|
|
0 bottles of beer on the wall, 0 bottles of beer! You take one down, |
122
|
|
|
|
|
|
|
and pass it around, 4294967295 bottles of beer on the wall! |
123
|
|
|
|
|
|
|
|; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
__END__ |