String::Interpolate(3pm) | User Contributed Perl Documentation | String::Interpolate(3pm) |
String::Interpolate - Wrapper for builtin the Perl interpolation engine.
# Functional interface use String::Interpolate qw( safe_interpolate interpolate ); our($GREET) = 'Hello'; # Cannot be lexical print interpolate( '$GREET $1\n', [ 'world' ] ); # Object interface use String::Interpolate; my $who; my $template = String::Interpolate->new( { WHO => \$who } ); $template->{TIME} = sub () { localtime }; # Tie $TIME to localtime() $template->( [ qw( now it ) ] ); # Set $1, $2 $template->[3] = '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 "$template\n"; # Perform interpolation # Peform the interpolation in a Safe compartment. my $replace = safe String::Interpolate '\u\L$1'; my $search = qr/(\w+)/; $_ = "HELLO world\n"; s/$search/$replace/eg; # /e suppresses 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 overridden 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.
If called as an instance method new() clones the object. Be aware, however, that this is a shallow cloning and if array or hash reference arguments have been passed to the object the parent and clone will continue to use the same array or hashes until one or other is passed a new argument.
Most of the other methods in String::Interpolate will implicitly call new() if called as class methods.
The argument list is passed to exec() as in new().
The safe() method can also be called on an existing object in which case it instructs the object to forget its current Safe compartment or namespace and use an automatically allocated temporary Safe compartment henceforth.
my $interpolated_string = $interpolate_object->exec; my $interpolated_string = "$interpolate_object"; my $interpolated_string = $interpolate_object->exec(LIST); my $interpolated_string = $interpolate_object->(LIST);
The exec() method modifies the object according the argument list. Then, if called in a non-void context, returns the result of the interpolation. Note that the modifications are persistent. This persistence can be avoided by creating a transient clone using the new() method.
my $string = $inter->(LIST); # $inter changed my $string = $inter->new->(LIST); # $inter unchanged
Also, if exec() is called as a class method then it acts on a temporary String::Interpolate object which is immediately destroyed.
The elements of the argument list are interpreted according to their type as listed below. If this mechanism does not provide sufficient flexibility in manipulating the symbol table you can, of course, manipulate it directly too.
After the object has been instructed to populate package variables in this way it will no longer default to using the namespace from which the constructor was called and will instead auto-allocate a temporary one unless told to do otherwise.
If multiple hash reference arguments are specified in a single call to exec() then each hash in turn will be processed prior to each interpolation. However, whenever a exec() is passed one or more hash references it forgets any previous hashes and deletes any auto-allocated temporary package or safe compartment.
The keys of the hash should be unqualified Perl identifiers that will determine the entries in the package symbol to be modified. Which slot in the symbol table entry is modified is determined by the values' types as follows:
Note that if interpolation is taking place inside a Safe compartment the callback will, by default, simply be called from within the compartment. The callback code will execute with a false symbol table root so it will not be able to use any packages from the real symbol table root. This limitation can be overcome by using the safe_hole() method.
See above for limitations if the callback is called from interpolation taking place in a Safe compartment.
The argument passed to the callback will be stringified. It may seem like a nice idea to be able to pass multiple arguments using an ARRAY reference but unfortunately this could open up security problems when passing arguments out of a Safe compartment via a Safe::Hole.
Note that since the String::Interpolate object stores a reference to the hash and updates the symbol table prior to each interpolation, changes in the hash will be reflected in subsequent interpolations. However, if items in the hash are deleted or changed to a different type then the previously created symbol table entries may persist. This can be overcome by calling the safe() or package() methods.
To simplify modifying the hash, a String::Interpolated object used in a HASH reference context will return a reference to the last hash argument passed to object, implicitly calling exec({}) first if necessary.
my %h = ( A => 1 ); my $i = String::Interpolate->new( \%h ); $i->{B} = 2; # $h{B} = 2
Passing a package argument to the object causes it to stop using a Safe compartment if it previously was doing so. If you want safe execution in a specific namespace then you need to explicitly constuct Safe object bound to the given namespace and pass that.
Once a String::Interpolate object has been explicitly bound to a namespace it will continue to use that namespace even if the String::Interpolate object has been (or is subsequently) passed a hash reference argument. In this case the symbols will be created/updated in the namespace prior to each interpolation and will persist afterwards.
See also the package() method.
Once a String::Interpolate object has been explicitly bound to a Safe object it will continue to use that object even if the String::Interpolate object has been (or is subsequently) passed a hash reference argument. In this case the symbols will be created/updated in the namespace associated with the Safe object prior to each interpolation and will persist afterwards.
See also the safe() method.
For those heathens who don't like the OO interface.
The following methods provide alternative interfaces and some fine tuning capabilities.
$i->trap; # Enable trapping $i->trap(1); # Enable trapping $i->trap(0); # Disable trapping
Returns the object so that it can be tagged on to constructor calls.
my $i = String::Interpolate->safe->trap(0);
If the trap(0) method has not been called then trapping is enabled when using a Safe compartment.
$i->unsafe_underscore; # Enable unsafe underscore mode $i->unsafe_underscore(1); # Enable unsafe underscore mode $i->unsafe_underscore(0); # Disable unsafe underscore mode
Returns the object so that it can be tagged on to constructor calls.
$i->unsafe_symbols; # Enable unsafe symbol mode $i->unsafe_symbols(1); # Enable unsafe symbol mode $i->unsafe_symbols(0); # Disable unsafe symbol mode
Returns the object so that it can be tagged on to constructor calls.
Tells the String::Interpolate object whether or not to use the PadWalker module to import all lexical variables from the calling context into the temporary package or Safe compartment. By default this does not happen as it is conceptually ugly and quite expensive.
$i->lexicals; # Enable lexicals $i->lexicals(1) # Enable lexicals $i->lexicals(0); # Disable lexicals
Returns the object so that it can be tagged on to constructor calls.
my $i = String::Interpolate->safe->lexicals;
Enabling lexicals with a Safe compartment like this will give the code read-only access to all your lexical variables.
Note that the lexicals used are those in scope at the final call that performs the interpolation, not those in scope when the String::Interpolate object is constructed. Also you can't have your cake and eat it. If you cannot use this feature at the same time as an explicit package or Safe compartment.
The package method Returns the object so that it can be tagged on to constructor calls. It can also be used as a constructor.
my $i = String::Interpolate->package('Q'); # Use namespace Q:: $i->package; # Use temporary namespace $i->package(*R); # Use namespace R:: $i->package(\*S::); # Use namespace S::
Note that the last two forms are not commonly used as GLOB or GLOB reference arguments passed to the exec(), new() or methods are automatically passed on the the package() method.
my $i = String::Interpolate->safe->safe_hole; # Without a Safe::Hole Wibble::wobble() would be inaccessible $i->{FOO} = sub () { Wibble->wobble };
This feature only makes sense when evaluating in a Safe compartment and you can only use it if you have the Safe::Hole module installed.
$i->safe_hole; # Enable use of Safe::Hole $i->safe_hole(1); # Enable use of Safe::Hole $i->safe_hole(0); # Disable use of Safe::Hole $i->safe_hole($hole); # Use the Safe::Hole object $hole
This method can also be called implicitly as follows.
$i->(\'SAFE HOLE'); # Enable use of Safe::Hole $i->(\'NO_SAFE_HOLE'); # Disable use of Safe::Hole $i->($hole); # Use the Safe::Hole object $hole
The safe_hole() method returns the object so that it can be tagged on to constructor calls.
For the most commonly used values, to control the handling of interpolating undefined values, the following shorthands can also be used:
NOWARN => 'unimport warnings qw(uninitialized)' WARN => '' FATAL => 'import warnings FATAL => qw(uninitialized); import strict qw(vars)'
The default state for a newly created String::Interpolate object is NOWARN. All other warnings are enabled as are 'refs' and 'subs' strictures.
You can call pragma() implicitly by passing SCALAR references to exec(). Furthermore pragma('TRAP') is a synonym for trap(1) and pragma('NO TRAP') is a synonym for trap(0). Similarly for lexicals(), unsafe_symbols(), unsafe_underscore() and safe_hole(). This makes the following statements equivalent:
$i->(\'FATAL',\'NO TRAP',\'SAFE SYMBOLS'); $i->pragma('FATAL','NO_TRAP','NO UNSAFE_SYMBOLS'); $i->pragma('FATAL')->trap(0)->unsafe_symbols(0);
The pragma() method returns the object so that it can be tagged on to constructor calls.
my @p = qw ( one two three ); my $i = String::Interpolate->new( \@p ); $i->positionals->[1] = "TWO"; # $p[1] = "TWO"; $i->positionals = [ qw ( X Y ) ]; # Forget @p, use anon array undef $i->positionals; # $1 etc. inherted from caller
String::Interpolate::Shell, String::Interpolate::RE, String::Expand, String::MatchInterpolate.
<https://github.com/neilb/String-Interpolate>
Created by Brian McCauley, currently maintained by Neil Bowers <neilb@cpan.org>
This software is copyright (c) 2002 by Brian McCauley.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
2021-03-27 | perl v5.32.1 |