package Archive::Builder::Generators; # This package contains a set of default generators # for the most common cases. use strict; use Params::Util '_INSTANCE', '_SCALAR0', '_HASH0'; use Archive::Builder (); use vars qw{$VERSION}; BEGIN { $VERSION = '1.14'; } ##################################################################### # Trvial Generators # Recieves as an argument the exact string the file should contain sub string { my $File = _INSTANCE(shift, 'Archive::Builder::File' ) or return undef; my $string = shift; return _SCALAR0($string) ? $string : ref $string ? undef : defined $string ? \$string : undef; }; # Recieves as an argument the name of a file sub file { my $File = _INSTANCE(shift, 'Archive::Builder::File') or return undef; my $filename = -f $_[0] ? shift : return undef; # Slurp in the file File::Flat->slurp( $filename ) or $File->_error( "Failed to load file '$filename'" ); } # Takes any object derived from class IO::Handle, reads it in # and returns it. An optional second argument is the number of bytes # to read in at a time ( the chunk size ). Default is 8192 ( 8 kilobytes ) sub handle { my $File = _INSTANCE(shift, 'Archive::Builder::File') or return undef; # Get and check the handle my $handle = _INSTANCE(shift, 'IO::Handle') or return $File->_error( 'Was not passed an IO::Handle argument' ); my $chunk_size = shift || (8 * 1024); # Read in everything my $contents = ''; my ($rv, $buffer); while ( $rv = $handle->sysread( $buffer, $chunk_size ) ) { $contents .= $buffer; } defined $rv ? \$contents : $File->_error( 'Error while reading from handle' ); } ##################################################################### # Common Advanced Generators # The template generator will only work if the Template Toolkit is installed. # The first argument is an instantiation of a Template object. # The second argument is the file name withing the Template object. # The third argument is the hash reference to pass to the template. sub template { my $File = _INSTANCE(shift, 'Archive::Builder::File' ) or return undef; # Before beginning, test to see if Template toolkit is installed unless ( Class::Autouse->load( 'Template' ) ) { return $File->_error( 'Template Toolkit is not installed, or could not be loaded' ); } # Get and check the arguments my $Template = _INSTANCE(shift, 'Template' ) or return $File->_error( 'First argument was not a Template object' ); my $toparse = shift or return $File->_error( 'You did not specify something to parse' ); my $args = (_HASH0($_[0]) || ! defined $_[0]) ? shift : return $File->_error( 'Invalid argument hashref for Template' ); # Create a string to capture the output into. my $output = ''; # Process the template $Template->process( $toparse, $args, \$output ) ? \$output : $File->_error( "Template Error: " . $Template->error ); } 1; __END__ =head1 NAME Archive::Builder::Generators - Default generators, and writing your own =head1 SYNOPSIS # Our own useless generator sub generator { my $File = isa( $_[0], 'Archive::Builder::File' ) ? shift : return undef; # Create the file contents my $contents = 'Something trivial'; return \$contents; } =head1 DESCRIPTION This documentation outlines the default generators available to you, and how to write generators of your own to extend L. =head1 DEFAULT GENERATORS A limited set of generators for the most common situation are provided for you. =head2 The 'string' Generator The 'string' default generator is a simple pass-through for when you already have the contents of the file, generated by another method. The generator takes one argument, which can be either a scalar containing the file contents, or a reference to a scalar containing the file contents =head2 The 'file' Generator The 'file' generator takes as an argument of a file name, and slurps in the file as the contents. It should be used when the builder file already exists on disk, and just needs to be used directly. Most commonly used for binary files like images and such, that might need to be included, but not modified. =head2 The 'handle' Generator The 'handle' generator takes as argument a single object of an IO::Handle object. It allows you use something the can only easily be accessed as an IO handle easily. The generator will read from the handle until an EOF is reached and then returns the results. =head2 The 'template' Generator The 'template' generator hooks in to the power of Template Toolkit. It takes three arguments. The first is a valid L