File Coverage

lib/Test/WWW/Mechanize/Driver/MagicValues.pm
Criterion Covered Total %
statement 46 46 100.0
branch 1 2 50.0
condition n/a
subroutine 12 12 100.0
pod n/a
total 59 60 98.3


line stmt bran cond sub pod time code
1              
2             =head1 NAME
3              
4             Test::WWW::Mechanize::Driver::MagicValues - Define Magic Value classes
5              
6             =head1 Magic Classes
7              
8             =cut
9              
10             =head2 FileContents
11              
12             Stringification of a scalar reference in this class will slurp the file
13             whose name was stored in the reference.
14              
15             my $file = "Foo.txt";
16             print bless(\$file, "FileContents"); # prints contents of Foo.txt
17              
18             =cut
19              
20             package FileContents;
21 9     9   68 use strict; use warnings;
  9     9   24  
  9         274  
  9         45  
  9         20  
  9         1029  
22             our $VERSION = 1.0;
23              
24             use overload '""' => sub {
25 2     2   771 my $x = shift;
26 2 50       86 open my $F, "<", $$x or die "Can't open $$x for reading: $!";
27 2         13 local $/;
28 2         93 return scalar <$F>;
29 9     9   65 };
  9         54  
  9         125  
30              
31              
32             =head2 ApplyTemplate
33              
34             Stringification of a scalar reference in this class will pass the text of
35             the reference through Template.pm. Some useful variables will be defined.
36              
37             my $tmpl = "[% now.strftime('%Y-%m-%d') %]";
38             print bless(\$tmpl, "ApplyTemplate"); # prints current date
39              
40             =over 4
41              
42             =item t
43              
44             The value of C<$Test::WWW::Mechanize::Driver::CURRENT_GROUP>
45              
46             =item now
47              
48             The current date as a DateTime object.
49              
50             =back
51              
52             =cut
53              
54             package ApplyTemplate;
55 9     9   782 use strict; use warnings;
  9     9   30  
  9         276  
  9         179  
  9         93  
  9         1322  
56             our $VERSION = 1.0;
57              
58             use overload '""' => sub {
59 1     1   3 my $x = shift;
60 1         641 require Template;
61 1         20915 require DateTime;
62 1         506455 my $template = Template->new();
63 1         23954 my $input = $$x;
64 1         3 my $output;
65 1         12 $template->process( \$input, {
66             t => $Test::WWW::Mechanize::Driver::CURRENT_GROUP,
67             now => DateTime->now,
68             },
69             \$output );
70 1         31264 return $output;
71 9     9   65 };
  9         23  
  9         72  
72              
73              
74             =head2 Stacked
75              
76             Stringification of a scalar reference in this class will slurp the file
77             whose name was stored in the reference.
78              
79             my @stack = ( "Foo.txt", "FileContents", "ApplyTemplate" );
80             print bless(\@stack, "Stacked"); # fills in and prints template in Foo.txt
81              
82             =cut
83              
84             package Stacked;
85 9     9   665 use strict; use warnings;
  9     9   18  
  9         196  
  9         44  
  9         29  
  9         1359  
86             our $VERSION = 1.0;
87              
88             use overload '""' => sub {
89 1     1   3 my $x = shift;
90 1         3 my $value = $$x[0];
91 1         5 for my $pkg (@$x[1..$#{$x}]) {
  1         5  
92 2         13 my $blessed = bless \$value, $pkg;
93 2         29 $value = "$blessed";
94             }
95 1         96 return $value;
96 9     9   72 };
  9         25  
  9         72  
97              
98              
99              
100             1;
101              
102             __END__
103              
104             =head1 AUTHOR
105              
106             Dean Serenevy
107             dean@serenevy.net
108             https://serenevy.net/
109              
110             =head1 COPYRIGHT
111              
112             This software is hereby placed into the public domain. If you use this
113             code, a simple comment in your code giving credit and an email letting
114             me know that you find it useful would be courteous but is not required.
115              
116             The software is provided "as is" without warranty of any kind, either
117             expressed or implied including, but not limited to, the implied warranties
118             of merchantability and fitness for a particular purpose. In no event shall
119             the authors or copyright holders be liable for any claim, damages or other
120             liability, whether in an action of contract, tort or otherwise, arising
121             from, out of or in connection with the software or the use or other
122             dealings in the software.
123              
124             =head1 SEE ALSO
125              
126             perl(1).