| 1 | # Revision $Revision$ ( $Date$ ) - $Source$ | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package WWW::NameGen; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | my $max = 2_500; | |||||||||||||||||||||||||||
| 13 | 2 | my $chunk_size = 250; | |||||||||||||||||||||||||||
| 14 | 2 | my @chunks; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 1 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 18 | 1 | my $self = bless {%args}, $class; | |||||||||||||||||||||||||||
| 19 | 1 | $self->{'ua'} = LWP::UserAgent->new; | |||||||||||||||||||||||||||
| 20 | 1 | return $self; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | sub get_chunks { | ||||||||||||||||||||||||||
|
Subroutine
get_chunks: called CC=3 SCAR=16.8
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 1 | my ($self, $count, @out) = @_; | |||||||||||||||||||||||||||
| 25 | 1 | ▶ | if ($count < $chunk_size) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 1 | push @out, $count; | |||||||||||||||||||||||||||
| 27 | } else { | ||||||||||||||||||||||||||||
| 28 | 0 | my $chunks = $count / $chunk_size; | |||||||||||||||||||||||||||
| 29 | 0 | my $remainder = $count % $chunk_size; | |||||||||||||||||||||||||||
| 30 | 0 | map { push @out, $chunk_size } 1 .. $chunks; | |||||||||||||||||||||||||||
| 31 | 0 | ▶ | if ($remainder) { push @out, $remainder; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 1 | return @out; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | sub generate { | ||||||||||||||||||||||||||
|
Subroutine
generate: called CC=10 SCAR=31.1
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 1 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | ▶ | my $min = $args{'min'} ? $args{'min'} : 10; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 1 | ▶ | my $obscurity = $args{'obscurity'} ? $args{'obscurity'} : 30; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | my $type = $args{'type'} ? $args{'type'} : 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # return cached results if we can | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | if (! $args{'nocache'} && $self->{'__cache'}) { return @{$self->{'__cache'}}; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $args{"nocache"} and $self->{"__cache"}
MC/DC0%0/2
not $args{"nocache"} and $self->{"__cache"}
not $args{"nocache"}
$self->{"__cache"}
Truth table33%1/3
not(A) and B
A= not $args{"nocache"}B= $self->{"__cache"}
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | my ($count, @names) = (0, qw//); | |||||||||||||||||||||||||||
| 47 | 1 | my @chunks = $self->get_chunks($min); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | for my $tcount (@chunks) { | |||||||||||||||||||||||||||
| 50 | 0 | ▶ | if ($count >= $max) { last; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | my $response = $self->{ua}->post( | ||||||||||||||||||||||||||||
| 52 | 1 | 'http://www.kleimo.com/random/name.cfm', | |||||||||||||||||||||||||||
| 53 | { | ||||||||||||||||||||||||||||
| 54 | 'type' => $type, | ||||||||||||||||||||||||||||
| 55 | 'number' => $tcount, | ||||||||||||||||||||||||||||
| 56 | 'obscurity' => $obscurity, | ||||||||||||||||||||||||||||
| 57 | 'Go' => 'Generate Random Name(s)', | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | ▶ | if (! $response->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 1 | die "Error getting data!\n"; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | my $html = $response->content; | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | while ($html =~ m!target="_blank">([^<]*)</a><br>!gixm ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | push @names, $1; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 0 | $count += $tcount; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | $self->{'__cache'} = \@names; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | return @names; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2 | 1; | |||||||||||||||||||||||||||
| 78 | __END__ | ||||||||||||||||||||||||||||