File Coverage

blib/lib/WWW/Mechanize/PhantomJS/DSL.pm
Criterion Covered Total %
statement 24 31 77.4
branch 1 4 25.0
condition 2 5 40.0
subroutine 7 7 100.0
pod n/a
total 34 47 72.3


line stmt bran cond sub pod time code
1             package WWW::Mechanize::PhantomJS::DSL;
2 1     1   68571 use strict;
  1         13  
  1         30  
3 1     1   724 use WWW::Mechanize::PhantomJS;
  1         4  
  1         38  
4 1     1   574 use Object::Import;
  1         25536  
  1         6  
5 1     1   46 use Carp qw(croak);
  1         3  
  1         54  
6              
7 1     1   6 use vars qw($VERSION @CARP_NOT);
  1         2  
  1         185  
8             $VERSION= '0.22';
9              
10             @CARP_NOT = (qw[
11             WWW::Mechanize::PhantomJS
12             ]);
13              
14             =head1 NAME
15              
16             WWW::Mechanize::PhantomJS::DSL - Domain Specific Language for short scripts
17              
18             =head1 SYNOPSIS
19              
20             use WWW::Mechanize::PhantomJS::DSL '$mech';
21              
22             get 'http://google.de';
23            
24             my @links = selector('a');
25             print $_->get_text(),"\n" for @links;
26            
27             click($links[0]);
28            
29             print content;
30              
31             This module exports all methods of one WWW::Mechanize::PhantomJS
32             object as subroutines. That way, you can write short scripts without
33             cluttering every line with C<< $mech-> >>.
34              
35             This module is highly experimental and might vanish from the distribution
36             again if I find that it is useless.
37              
38             =cut
39              
40             sub import {
41 1     1   11 my ($class, %options);
42 1 50       4 if (@_ == 2) {
43 0         0 ($class, $options{ name }) = @_;
44             } else {
45 1         5 ($class, %options) = @_;
46             };
47 1   33     9 my $target = delete $options{ target } || caller;
48 1   50     10 my $name = delete $options{ name } || '$mech';
49 1         9 my $mech = WWW::Mechanize::PhantomJS->new(%options);
50            
51 0 0         $name =~ s/^[\$]//
52             or croak 'Variable name must start with $';
53             {
54 1     1   11 no strict 'refs';
  1         2  
  1         116  
  0            
55 0           *{"$target\::$name"} = \$mech;
  0            
56 0           import Object::Import \${"$target\::$name"},
  0            
57             deref => 1,
58             target => $target,
59             ;
60             };
61             };
62              
63             1;
64              
65             =head1 AUTHORS
66              
67             Max Maischein C
68              
69             =head1 COPYRIGHT (c)
70              
71             Copyright 2009-2015 by Max Maischein C.
72              
73             =head1 LICENSE
74              
75             This module is released under the same terms as Perl itself.
76              
77             =cut