File Coverage

blib/lib/Template/Patch.pm
Criterion Covered Total %
statement 52 56 92.8
branch 15 24 62.5
condition n/a
subroutine 9 10 90.0
pod 0 4 0.0
total 76 94 80.8


line stmt bran cond sub pod time code
1             package Template::Patch;
2              
3 2     2   50826 use warnings;
  2         7  
  2         63  
4 2     2   11 use strict;
  2         4  
  2         65  
5              
6 2     2   2021 use Template::Extract;
  2         1394  
  2         51  
7 2     2   1885 use Template;
  2         59012  
  2         65  
8              
9 2     2   22 use base 'Class::Accessor::Ref';
  2         3  
  2         1931  
10              
11             our $VERSION = '0.03';
12              
13             BEGIN {
14 2     2   8149 my @accs = (qw/ inp outp vars routput rinput _ext _tt conf/);
15 2         19 __PACKAGE__->mk_accessors(@accs);
16 2         692 __PACKAGE__->mk_refaccessors(@accs);
17             }
18              
19             =head1 NAME
20              
21             Template::Patch - Apply parameterized patches
22              
23             =head1 SYNOPSIS
24              
25             $ metapatch --patch mychanges.mp < oldfile > newfile
26              
27             # or, programmatically:
28              
29             use Template::Patch;
30              
31             my $tp = Template::Patch->parse_patch_file($metapatch_file);
32             $tp->extract($source);
33             $tp->patch;
34             $tp->print;
35              
36             =head1 DESCRIPTION
37              
38             Please see L for documentation. This module is experimental and
39             the API here is subject to change.
40              
41             =head1 FUNCTIONS
42              
43             This isn't very streamlined yet, and is subject to change.
44              
45             =cut
46              
47             sub new_from_file {
48 3     3 0 6495 my($class, $pfile) = @_;
49 3         7 my($to, $from);
50              
51 3 50       12 die "$0: must supply --patch arg" unless defined $pfile;
52              
53 3         38 my $self = $class->new( { vars => {},
54 3         8 conf => {}, routput => do{\my $output_port} } );
55              
56 3 100       293 open my $fh, "<", $pfile or die "$0: open: $pfile: $!";
57 2         45 while (<$fh>) {
58 8 100       75 if (!$from) {
59 2 50       19 $from++, next if /^<{20}/;
60 0 0       0 next if /^#/;
61 0 0       0 $self->conf->{$1} = $2 if /([^:]+) \s* : \s* (.*?) \s* $/x;
62             }
63              
64 6 100       27 $to++, next if /^>{20}/;
65              
66 4 100       5 ${ $self->get_ref($to ? 'outp' : 'inp' ) } .= $_;
  4         28  
67             }
68 2 50       54 die "$0: $pfile: no output template" unless $self->outp;
69              
70              
71             # conf-related fixups
72             # xxx: higher-order this, ew
73 2 50       45 if (! $self->conf->{'anchor-start'}) {
74 2         26 for my $tname (qw/ inp outp /) {
75 4         14 my $tref = $self->get_ref($tname);
76 4         58 $$tref = "[% pre %]" . $$tref;
77             }
78             }
79 2 50       9 if (! $self->conf->{'anchor-end'}) {
80 2         21 for my $tname (qw/ inp outp /) {
81 4         12 my $tref = $self->get_ref($tname);
82 4         51 chomp $$tref;
83 4         11 $$tref .= "[% post %]";
84             }
85             }
86              
87             #::YY($self);
88 2         47 return $self;
89             }
90              
91             sub extract {
92 2     2 0 1458 my($self, $input) = @_;
93 2         17 $self->_ext( Template::Extract->new );
94 2         47713 $self->_ext->extract(
95             $self->inp, # input template
96             $input, # actual data to parse
97             $self->vars, # dictionary for extracted data
98             );
99             # we need to keep a ref to input around for the case where no extraction
100             # was successful.
101 2         4997 $self->rinput(\$input);
102             #::YY($self->vars);
103             }
104              
105             sub patch {
106 2     2 0 949 my($self) = @_;
107              
108             # if the dictionary is empty, extract didn't find anything.
109             # copy over the input, so we don't emit just a broken template.
110             # XXX: copy or ref?
111 2 100       5 if (0 == keys %{ $self->vars }) {
  2         11  
112 1         16 $self->routput( $self->rinput );
113 1         21 return;
114             }
115              
116 1         41 $self->_tt( Template->new );
117 1         64529 $self->_tt->process( \$self->outp, $self->vars, $self->routput )
118             }
119              
120 0     0 0   sub print { print ${ $_[0]->routput } }
  0            
121              
122             #sub ::Y { require YAML::Syck; YAML::Syck::Dump(@_) }
123             #sub ::YY { require Carp; Carp::confess(::Y(@_)) }
124              
125             =head1 SEE ALSO
126              
127             =over 4
128              
129             =item L
130              
131             =item L
132              
133             =item L
134              
135             =back
136              
137             =head1 AUTHOR
138              
139             Gaal Yahas, C<< >>
140              
141             =head1 CAVEATS
142              
143             This module and the included C tool are in early stages of
144             gathering ideas and coming up with a good interface. They work (and have
145             saved me time), but expect change in the interfaces.
146              
147             =head1 BUGS
148              
149             Please report any bugs or feature requests to
150             C, or through the web interface at
151             L.
152             I will be notified, and then you'll automatically be notified of progress on
153             your bug as I make changes.
154              
155             =head1 SUPPORT
156              
157             You can find documentation for this module with the perldoc command.
158              
159             perldoc Template::Patch
160              
161             You can also look for information at:
162              
163             =over 4
164              
165             =item * AnnoCPAN: Annotated CPAN documentation
166              
167             L
168              
169             =item * CPAN Ratings
170              
171             L
172              
173             =item * RT: CPAN's request tracker
174              
175             L
176              
177             =item * Search CPAN
178              
179             L
180              
181             =back
182              
183             =head1 ACKNOWLEDGEMENTS
184              
185             Thanks to Audrey Tang for sausage machine (and general) havoc.
186              
187             =head1 COPYRIGHT & LICENSE
188              
189             Copyright 2006 Gaal Yahas, all rights reserved.
190              
191             This program is free software; you can redistribute it and/or modify it
192             under the same terms as Perl itself.
193              
194             =cut
195              
196             1; # End of Template::Patch