#!perl # # Documentation, copyright and license is at the end of this file. # package Text::Replace; use 5.001; use strict; use warnings; use warnings::register; use vars qw($VERSION $DATE $FILE); $VERSION = '1.13'; $DATE = '2004/05/11'; $FILE = __FILE__; use vars qw(@ISA @EXPORT_OK); require Exporter; @ISA=('Exporter'); @EXPORT_OK = qw(&replace_variables); ####### # Replace variables in template # sub replace_variables { ###### # This subroutine uses no object data; therefore, # drop any class or object. # shift @_ if UNIVERSAL::isa($_[0],__PACKAGE__); my ($template_p, $hash_p, $variables_p) = @_; unless( $variables_p ) { my @keys = sort keys %$hash_p; $variables_p = \@keys; } ######### # Substitute selected content macros # my $count = 1; while( $count ) { $count = 0; foreach my $variable (@$variables_p) { $count += $$template_p =~ s/([^\\])\$\{$variable\}/${1}$hash_p->{$variable}/g; } } $$template_p =~ s/\\\$/\$/g; # unescape macro dollar 1; } 1 __END__ =head1 NAME Text::Replace - replace variables from a hash =head1 SYNOPSIS ####### # Subroutine Interface # use Text::Replace qw(&replace_variables); $success = replace_variables(\$template, \%variable_hash, \@variable); ######## # Class Interface # use Text::Replace; $success = Text::Replace->replace_variables(\$template, \%variable_hash, \@variable); =head1 DESCRIPTION The C program module is simple and plain by design. The C program module mimics the built-in Perl double quote, '"', literal scalar that replaces Perl scalar variables named with a leading '$'. The C program module stays in the background, just like John William's movie scores. There is no large manual thicker than the Bible with tricks and tips and gyrations to learn and to distract. The module is plain, simple with no expressiveness. The C program module does only one thing: substitute a value for a variable. It is amazing how many times, just a simple double quote literal replacement in a small string or even a large text string gets the job done. Does C solve all variable replacement, template problems? Definitely not. There is no capabilities for inserting graphs, text wrap plug-ins, GD interface. If an application needs something this sophisticated, there are many fine template program modules in CPAN such as the highly rated C