String::Interpolate

String::Interpolate is a Perl wrapper for builtin the Perl interpolation engine.
Download

String::Interpolate Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Brian McCauley
  • Publisher web site:
  • http://search.cpan.org/~nobull/String-Interpolate-0.3/lib/String/Interpolate.pm

String::Interpolate Tags


String::Interpolate Description

String::Interpolate is a Perl wrapper for builtin the Perl interpolation engine. SYNOPSIS # Functional interface use String::Interpolate qw( safe_interpolate interpolate ); our($GREET) = 'Hello'; # Cannot be lexical print interpolate( '$GREET $1n', ); # Object interface use String::Interpolate; my $who; my $template = new String::Interpolate { WHO => $who }; $template->{TIME} = sub () { localtime }; # Tie $TIME to localtime() $template->( ); # Set $1, $2 $template-> = 'is'; # Sets $3 $who = 'old friend'; $template->( '$REV{olleH} $WHO, $2 $3 $1 $TIME$_' ); # Set string to process $template->{REV} = sub { reverse @_ }; # Tie %REV to reverse() $_ = '.'; print "$templaten"; # Perform interpolation # Peform the interpolation in a Safe compartment. my $replace = safe String::Interpolate 'uL$1'; my $search = qr/(w+)/; $_ = "HELLO worldn"; s/$search/$replace/eg; # /e supresses optimisation print;String::Interpolate provides a neat interface to the solution to that perenial Perl problem - how to invoke the Perl string interpolation engine on a string contained in a scalar variable.A String::Interpolate object encapsulates a string and a context in which it should be subjected to Perl interpolation. In the simplest, default, case the context is simply the namespace (package) from which the constructor was called.A String::Interpolate object may hold a reference to an array and hashes that will be used to populate the special variables $1 etc and some package variables respectively prior to each interpolation.In general special globally global variables such as $_ can be used in the interpolation, the exception being @_ which is always empty during the interpolation.The interpolated string is processed with strictures and warnings enabled excluding 'strict vars' and 'warnings uninitialized' so that interpolating undefined variables will be silently ignored. This behaviour can be altered using the pragma() method.Because the Perl string interpolation engine can call arbitrary Perl code you do not want to want to use it on strings from untrusted sources without some precautions. For this reason String::Interpolate objects can be made to use Safe compartments. This is, of course, only as safe as Safe and you are advised to read "WARNING" section of the Safe documentation.When interpolating in a Safe compartment package symbols are imported using tied wrapper variables so that their values cannot be interpreted as references and such that they cannot be used to alter the values outside the compartment. This behaviour can be suppressed by the unsafe_symbols() method. Note that if you want to import tied variable or variables containing references to objects that use overloading into a Safe compartment then you will need to do a lot of fancy footwork unless you use safe_hole() method.By default *_ is shared by Safe compartments and could potentially allow the compartment to leak. The $_ and %_ variables are therefore subjected to the same similar precautions to imported symbols. This behaviour can be suppressed using the unsafe_underscore() method.Perl string interpolation can, of course, throw exceptions. By default String::Interpolate objects do not catch (or rethrow) these exceptions when working in a simple namespace and do trap them when working in a Safe compartment. This behaviour can be overriden by the trap() or pragma() methods. If an exception during interpolation is trapped then undef will be returned as the result of the interpolation and $@ will hold the exception in the usual way.When taint checking enabled, attempting to perform interpolation (using eval()) on a tainted string would naturally fail. However, when using a Safe compartment, String::Interpolate will strip the tainting off of the string prior to interpolation and put it back afterwards. Also String::Interpolate will taint any arguments passed to callback functions called as the result of performing interpolation on a tainted string. Note that due to the mechanism used to assign $1 et al they can never be tained even if the values in the array being used to set them are tainted.By default String::Interpolate does not export any subroutines but as a concession to programmers who prefer not to explicitly use objects the functions interpolate() and safe_interpolate() are exportable.Requirements:· Perl Requirements: · Perl


String::Interpolate Related Software