File Coverage

blib/lib/WWW/Mechanize/PhantomJS/DSL.pm
Criterion Covered Total %
statement 21 28 75.0
branch 1 4 25.0
condition 2 5 40.0
subroutine 6 6 100.0
pod n/a
total 30 43 69.7


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