File Coverage

blib/lib/WWW/Mechanize/Firefox/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::Firefox::DSL;
2 1     1   12969 use strict;
  1         1  
  1         23  
3 1     1   680 use WWW::Mechanize::Firefox;
  1         1  
  1         33  
4 1     1   595 use Object::Import;
  1         19150  
  1         5  
5 1     1   38 use Carp qw(croak);
  1         1  
  1         57  
6              
7 1     1   4 use vars qw($VERSION @CARP_NOT);
  1         1  
  1         128  
8             $VERSION = '0.79';
9              
10             @CARP_NOT = (qw[
11             WWW::Mechanize::Firefox
12             ]);
13              
14             =head1 NAME
15              
16             WWW::Mechanize::Firefox::DSL - Domain Specific Language for short scripts
17              
18             =head1 SYNOPSIS
19              
20             use WWW::Mechanize::Firefox::DSL '$mech';
21              
22             get 'http://google.de';
23            
24             my @links = selector('a');
25             print $_->{innerHTML},"\n" for @links;
26            
27             click($links[0]);
28            
29             print content;
30              
31             This module exports all methods of one WWW::Mechanize::Firefox
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   7 my ($class, %options);
42 1 50       3 if (@_ == 2) {
43 0         0 ($class, $options{ name }) = @_;
44             } else {
45 1         3 ($class, %options) = @_;
46             };
47 1   33     6 my $target = delete $options{ target } || caller;
48 1   50     4 my $name = delete $options{ name } || '$mech';
49 1         6 my $mech = WWW::Mechanize::Firefox->new(%options);
50            
51 0 0         $name =~ s/^[\$]//
52             or croak 'Variable name must start with $';
53             {
54 1     1   4 no strict 'refs';
  1         1  
  1         79  
  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-2014 by Max Maischein C.
72              
73             =head1 LICENSE
74              
75             This module is released under the same terms as Perl itself.
76              
77             =cut