File Coverage

blib/lib/Archive/Builder/Generators.pm
Criterion Covered Total %
statement 27 35 77.1
branch 11 32 34.3
condition 1 5 20.0
subroutine 8 9 88.8
pod 0 4 0.0
total 47 85 55.2


line stmt bran cond sub pod time code
1             package Archive::Builder::Generators;
2              
3             # This package contains a set of default generators
4             # for the most common cases.
5              
6 7     7   42 use strict;
  7         14  
  7         341  
7 7     7   39 use Params::Util qw{ _INSTANCE _SCALAR0 _HASH0 };
  7         15  
  7         579  
8 7     7   40 use Archive::Builder ();
  7         13  
  7         196  
9              
10 7     7   36 use vars qw{$VERSION};
  7         16  
  7         367  
11             BEGIN {
12 7     7   5253 $VERSION = '1.16';
13             }
14              
15              
16              
17              
18              
19             #####################################################################
20             # Trvial Generators
21              
22             # Recieves as an argument the exact string the file should contain
23             sub string {
24 7 50   7 0 49 my $File = _INSTANCE(shift, 'Archive::Builder::File' ) or return undef;
25 7         13 my $string = shift;
26 7 50       47 return _SCALAR0($string) ? $string
    50          
    100          
27             : ref $string ? undef
28             : defined $string ? \$string
29             : undef;
30             };
31              
32             # Recieves as an argument the name of a file
33             sub file {
34 2 50   2 0 19 my $File = _INSTANCE(shift, 'Archive::Builder::File') or return undef;
35 2 50       34 my $filename = -f $_[0] ? shift : return undef;
36              
37             # Slurp in the file
38 2 50       14 File::Flat->slurp( $filename )
39             or $File->_error( "Failed to load file '$filename'" );
40             }
41              
42             # Takes any object derived from class IO::Handle, reads it in
43             # and returns it. An optional second argument is the number of bytes
44             # to read in at a time ( the chunk size ). Default is 8192 ( 8 kilobytes )
45             sub handle {
46 2 50   2 0 19 my $File = _INSTANCE(shift, 'Archive::Builder::File') or return undef;
47              
48             # Get and check the handle
49 2 50       27 my $handle = _INSTANCE(shift, 'IO::Handle')
50             or return $File->_error( 'Was not passed an IO::Handle argument' );
51 2   50     14 my $chunk_size = shift || (8 * 1024);
52              
53             # Read in everything
54 2         4 my $contents = '';
55 2         2 my ($rv, $buffer);
56 2         25 while ( $rv = $handle->sysread( $buffer, $chunk_size ) ) {
57 2         62 $contents .= $buffer;
58             }
59              
60 2 50       26 defined $rv ? \$contents
61             : $File->_error( 'Error while reading from handle' );
62             }
63              
64              
65              
66              
67              
68             #####################################################################
69             # Common Advanced Generators
70              
71             # The template generator will only work if the Template Toolkit is installed.
72             # The first argument is an instantiation of a Template object.
73             # The second argument is the file name withing the Template object.
74             # The third argument is the hash reference to pass to the template.
75             sub template {
76 0 0   0 0   my $File = _INSTANCE(shift, 'Archive::Builder::File' ) or return undef;
77              
78             # Before beginning, test to see if Template toolkit is installed
79 0 0         unless ( Class::Autouse->load( 'Template' ) ) {
80 0           return $File->_error( 'Template Toolkit is not installed, or could not be loaded' );
81             }
82            
83             # Get and check the arguments
84 0 0         my $Template = _INSTANCE(shift, 'Template' )
85             or return $File->_error( 'First argument was not a Template object' );
86 0 0         my $toparse = shift
87             or return $File->_error( 'You did not specify something to parse' );
88 0 0 0       my $args = (_HASH0($_[0]) || ! defined $_[0]) ? shift
89             : return $File->_error( 'Invalid argument hashref for Template' );
90            
91             # Create a string to capture the output into.
92 0           my $output = '';
93            
94             # Process the template
95 0 0         $Template->process( $toparse, $args, \$output ) ? \$output
96             : $File->_error( "Template Error: " . $Template->error );
97             }
98              
99             1;
100              
101             __END__