.\" Automatically generated by Pod::Man 4.10 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "String::Interpolate 3pm" .TH String::Interpolate 3pm "2020-02-10" "perl v5.28.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" String::Interpolate \- Wrapper for builtin the Perl interpolation engine. .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 4 \& # Functional interface \& use String::Interpolate qw( safe_interpolate interpolate ); \& our($GREET) = \*(AqHello\*(Aq; # Cannot be lexical \& print interpolate( \*(Aq$GREET $1\en\*(Aq, [ \*(Aqworld\*(Aq ] ); \& \& # Object interface \& use String::Interpolate; \& my $who; \& my $template = String::Interpolate\->new( { WHO => \e$who } ); \& $template\->{TIME} = sub () { localtime }; # Tie $TIME to localtime() \& $template\->( [ qw( now it ) ] ); # Set $1, $2 \& $template\->[3] = \*(Aqis\*(Aq; # Sets $3 \& $who = \*(Aqold friend\*(Aq; \& $template\->( \*(Aq$REV{olleH} $WHO, $2 $3 $1 $TIME$_\*(Aq ); # Set string to process \& $template\->{REV} = sub { reverse @_ }; # Tie %REV to reverse() \& $_ = \*(Aq.\*(Aq; \& print "$template\en"; # Perform interpolation \& \& # Peform the interpolation in a Safe compartment. \& my $replace = safe String::Interpolate \*(Aq\eu\eL$1\*(Aq; \& my $search = qr/(\ew+)/; \& $_ = "HELLO world\en"; \& s/$search/$replace/eg; # /e suppresses optimisation \& print; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" \&\f(CW\*(C`String::Interpolate\*(C'\fR 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. .PP A \f(CW\*(C`String::Interpolate\*(C'\fR 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. .PP A \f(CW\*(C`String::Interpolate\*(C'\fR object may hold a reference to an array and hashes that will be used to populate the special variables \f(CW$1\fR etc and some package variables respectively prior to each interpolation. .PP In general special globally global variables such as \f(CW$_\fR can be used in the interpolation, the exception being \f(CW@_\fR which is always empty during the interpolation. .PP 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 \fBpragma()\fR method. .PP 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 \&\f(CW\*(C`String::Interpolate\*(C'\fR objects can be made to use \f(CW\*(C`Safe\*(C'\fR compartments. This is, of course, only as safe as Safe and you are advised to read \*(L"\s-1WARNING\*(R"\s0 section of the Safe documentation. .PP 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 \fBunsafe_symbols()\fR 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 \fBsafe_hole()\fR method. .PP By default *_ is shared by Safe compartments and could potentially allow the compartment to leak. The \f(CW$_\fR and \f(CW%_\fR variables are therefore subjected to the same similar precautions to imported symbols. This behaviour can be suppressed using the \fBunsafe_underscore()\fR method. .PP 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 \&\fBtrap()\fR or \fBpragma()\fR 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. .PP When taint checking enabled, attempting to perform interpolation (using \fBeval()\fR) 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 \f(CW$1\fR et al they can never be tained even if the values in the array being used to set them are tainted. .PP By default \f(CW\*(C`String::Interpolate\*(C'\fR does not export any subroutines but as a concession to programmers who prefer not to explicitly use objects the functions \fBinterpolate()\fR and \fBsafe_interpolate()\fR are exportable. .SS "Principle methods" .IX Subsection "Principle methods" .IP "new" 4 .IX Item "new" Simple constructor. Creates a empty String::Interpolate object bound to the caller's namespace and then modifies the object by passing any arguments to the \fBexec()\fR method. Returns a the object. .Sp If called as an instance method \fBnew()\fR 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. .Sp Most of the other methods in String::Interpolate will implicitly call \&\fBnew()\fR if called as class methods. .IP "safe" 4 .IX Item "safe" Alternative constructor to create a String::Interpolate object that uses an automatically allocated temporary Safe compartment. The automatically allocated Safe compartment will have the default opcode mask but with the 'bless' opcode denied as this can be used to execute code outside the compartment by putting it in \s-1DESTROY\s0 methods. The \&'tie' opcode is also denied although I'm not sure if it really can be exploited in this way. There is no point explicitly passing a package or existing safe compartment to this constructor as it will be ignored. .Sp The argument list is passed to \fBexec()\fR as in \fBnew()\fR. .Sp The \fBsafe()\fR 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. .IP "exec" 4 .IX Item "exec" This it the guts of the implementation but it it rarely needs to be called explicitly as it can be more elegantly called implicitly by using the String::Interpolate object in a string or \s-1CODE\s0 reference context. The following are equivalent pairs: .Sp .Vb 2 \& my $interpolated_string = $interpolate_object\->exec; \& my $interpolated_string = "$interpolate_object"; \& \& my $interpolated_string = $interpolate_object\->exec(LIST); \& my $interpolated_string = $interpolate_object\->(LIST); .Ve .Sp The \fBexec()\fR 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 \&\fBnew()\fR method. .Sp .Vb 2 \& my $string = $inter\->(LIST); # $inter changed \& my $string = $inter\->new\->(LIST); # $inter unchanged .Ve .Sp Also, if \fBexec()\fR is called as a class method then it acts on a temporary String::Interpolate object which is immediately destroyed. .Sp 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. .RS 4 .IP "\s-1ARRAY\s0 reference" 4 .IX Item "ARRAY reference" Tells the object to use this array to populate the special variables \&\f(CW$1\fR and so on. The object holds a reference to the array itself and will use the values that are in the array at the time of interpolation. This \s-1ARRAY\s0 reference is exposed via the \fBpositionals()\fR method. The array can also be modified by using the String::Interpolate object in an \s-1ARRAY\s0 reference context. Note, however, that the String::Interpolate object used in an \s-1ARRAY\s0 reference context does not refer to the array itself but to a STORE-only tied array whose subscripts are offset by one such that \&\f(CW$interpolate_object\fR\->[1] corresponds to \&\f(CW$interpolate_object\fR\->positionals\->[0] and hence the value that will be interpolated for \f(CW$1\fR. .IP "\s-1HASH\s0 reference" 4 .IX Item "HASH reference" Tells the object to use this hash to populate some package variables immediately prior to each interpolation. The object holds a reference to the hash itself and will use the values that are in the hash at the time of interpolation. .Sp 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. .Sp If multiple hash reference arguments are specified in a single call to \&\fBexec()\fR then each hash in turn will be processed prior to each interpolation. However, whenever a \fBexec()\fR is passed one or more hash references it forgets any previous hashes and deletes any auto-allocated temporary package or safe compartment. .Sp 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: .RS 4 .IP "\s-1ARRAY\s0 reference" 4 .IX Item "ARRAY reference" Set the symbol table entry's \s-1ARRAY\s0 slot. .IP "\s-1HASH\s0 reference" 4 .IX Item "HASH reference" Set the symbol table entry's \s-1HASH\s0 slot. .IP "\s-1SCALAR\s0 reference" 4 .IX Item "SCALAR reference" Set the symbol table entry's \s-1SCALAR\s0 slot. .IP "\s-1CODE\s0 reference with prototype ()" 4 .IX Item "CODE reference with prototype ()" Set the symbol table entry's \s-1SCALAR\s0 slot to point to an new tied scalar with a \s-1FETCH\s0 method that calls the referenced code. .Sp 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 \fBsafe_hole()\fR method. .IP "\s-1CODE\s0 reference with prototype ($) or no prototype" 4 .IX Item "CODE reference with prototype ($) or no prototype" Set the symbol table entry's \s-1HASH\s0 slot to point to an new tied hash with a \s-1FETCH\s0 method that calls the referenced code. .Sp See above for limitations if the callback is called from interpolation taking place in a Safe compartment. .Sp 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 \s-1ARRAY\s0 reference but unfortunately this could open up security problems when passing arguments out of a Safe compartment via a Safe::Hole. .IP "Anything else" 4 .IX Item "Anything else" Set the symbol table entry's \s-1SCALAR\s0 slot to point scalar containing the value. .RE .RS 4 .Sp 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 \fBsafe()\fR or \fBpackage()\fR methods. .Sp To simplify modifying the hash, a String::Interpolated object used in a \s-1HASH\s0 reference context will return a reference to the last hash argument passed to object, implicitly calling exec({}) first if necessary. .Sp .Vb 3 \& my %h = ( A => 1 ); \& my $i = String::Interpolate\->new( \e%h ); \& $i\->{B} = 2; # $h{B} = 2 .Ve .RE .IP "\s-1GLOB\s0 or \s-1GLOB\s0 reference" 4 .IX Item "GLOB or GLOB reference" Instruct the object to perform interpolation in the namespace defined by the \s-1GLOB.\s0 For example the argument *Q:: would mean that the string should be interpolated in the context of the package Q. The trailing \&'::' may be omitted. .Sp 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. .Sp 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. .Sp See also the \fBpackage()\fR method. .IP "Safe object" 4 .IX Item "Safe object" Instruct the object to perform interpolation in the given Safe compartment. Passing a Safe object argument to the String::Interpolate object causes it to stop using a specified namespace if it previously was doing so. If you choose to pass an explicit Safe object you should deny the 'bless' and 'tie' opcodes for the reasons discussed under the \fBsafe()\fR method. .Sp 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. .Sp See also the \fBsafe()\fR method. .IP "Safe::Hole object" 4 .IX Item "Safe::Hole object" Equivalent to calling the \fBsafe_hole()\fR method with the same argument. .IP "\s-1SCALAR\s0 reference" 4 .IX Item "SCALAR reference" The referenced scalar is passed to the \fBpragma()\fR method. .IP "Anything else" 4 .IX Item "Anything else" Use the stringified value of the argument as the string on which to perform interpolation. .RE .RS 4 .RE .SS "Functional interface" .IX Subsection "Functional interface" For those heathens who don't like the \s-1OO\s0 interface. .IP "safe_interpolate" 4 .IX Item "safe_interpolate" Exportable function equivalent to String::Interpolate\->safe\->exec(\s-1LIST\s0). .IP "interpolate" 4 .IX Item "interpolate" Exportable function equivalent to String::Interpolate\->lexicals\->exec(\s-1LIST\s0). .SS "Ancillary methods" .IX Subsection "Ancillary methods" The following methods provide alternative interfaces and some fine tuning capabilities. .IP "trap" 4 .IX Item "trap" Tells the String::Interpolate object whether or not to trap exceptions. .Sp .Vb 3 \& $i\->trap; # Enable trapping \& $i\->trap(1); # Enable trapping \& $i\->trap(0); # Disable trapping .Ve .Sp Returns the object so that it can be tagged on to constructor calls. .Sp .Vb 1 \& my $i = String::Interpolate\->safe\->trap(0); .Ve .Sp If the \fBtrap\fR\|(0) method has not been called then trapping is enabled when using a Safe compartment. .IP "unsafe_underscore" 4 .IX Item "unsafe_underscore" Tells the String::Interpolate object whether or not to use \*(L"unsafe underscore\*(R" mode. In this mode no precautions are taken to prevent malicious code attempting to reach outside it's Safe compartment through the \f(CW$_\fR and \f(CW%_\fR variables. .Sp .Vb 3 \& $i\->unsafe_underscore; # Enable unsafe underscore mode \& $i\->unsafe_underscore(1); # Enable unsafe underscore mode \& $i\->unsafe_underscore(0); # Disable unsafe underscore mode .Ve .Sp Returns the object so that it can be tagged on to constructor calls. .IP "unsafe_symbols" 4 .IX Item "unsafe_symbols" Tells the String::Interpolate object whether or not to use \*(L"unsafe symbol\*(R" mode. In this mode variables are simply shared with the Safe compartment rather than being safely hidden behind variables tied to blessed closures. The setting of this flag as no effect when not using a Safe compartment. .Sp .Vb 3 \& $i\->unsafe_symbols; # Enable unsafe symbol mode \& $i\->unsafe_symbols(1); # Enable unsafe symbol mode \& $i\->unsafe_symbols(0); # Disable unsafe symbol mode .Ve .Sp Returns the object so that it can be tagged on to constructor calls. .IP "lexicals" 4 .IX Item "lexicals" This feature is \s-1EXPERIMENTAL.\s0 Do not use it in real code. .Sp 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. .Sp .Vb 3 \& $i\->lexicals; # Enable lexicals \& $i\->lexicals(1) # Enable lexicals \& $i\->lexicals(0); # Disable lexicals .Ve .Sp Returns the object so that it can be tagged on to constructor calls. .Sp .Vb 1 \& my $i = String::Interpolate\->safe\->lexicals; .Ve .Sp Enabling lexicals with a Safe compartment like this will give the code read-only access to all your lexical variables. .Sp 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. .IP "package" 4 .IX Item "package" Instructs the String::Interpolate object to forget its current Safe compartment or namespace and use the specified one henceforth. The package name can be specified as a string, a \s-1GLOB\s0 or a \s-1GLOB\s0 reference. The trailing '::' may be omitted. With an undefined argument this method instructs the object to use a new automatically allocated temporary namespace. .Sp The package method Returns the object so that it can be tagged on to constructor calls. It can also be used as a constructor. .Sp .Vb 4 \& my $i = String::Interpolate\->package(\*(AqQ\*(Aq); # Use namespace Q:: \& $i\->package; # Use temporary namespace \& $i\->package(*R); # Use namespace R:: \& $i\->package(\e*S::); # Use namespace S:: .Ve .Sp Note that the last two forms are not commonly used as \s-1GLOB\s0 or \s-1GLOB\s0 reference arguments passed to the \fBexec()\fR, \fBnew()\fR or methods are automatically passed on the the \fBpackage()\fR method. .IP "safe_hole" 4 .IX Item "safe_hole" Tells the String::Interpolate object whether or not to use a Safe::Hole object to wrap callbacks to subroutines specified in the symbol mapping hash. Without a Safe::Hole \fBeval()\fR, symbolic references and method calls in callbacks won't function normally. .Sp .Vb 3 \& my $i = String::Interpolate\->safe\->safe_hole; \& # Without a Safe::Hole Wibble::wobble() would be inaccessible \& $i\->{FOO} = sub () { Wibble\->wobble }; .Ve .Sp 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. .Sp .Vb 4 \& $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 .Ve .Sp This method can also be called implicitly as follows. .Sp .Vb 3 \& $i\->(\e\*(AqSAFE HOLE\*(Aq); # Enable use of Safe::Hole \& $i\->(\e\*(AqNO_SAFE_HOLE\*(Aq); # Disable use of Safe::Hole \& $i\->($hole); # Use the Safe::Hole object $hole .Ve .Sp The \fBsafe_hole()\fR method returns the object so that it can be tagged on to constructor calls. .IP "pragma" 4 .IX Item "pragma" Specify various options including Perl code to be complied in a BEGIN{} block prior to compiling the string to be interpolated. When working in a Safe compartment, what you can do here is, of course, highly limited. In practice this is only useful for calling the \&\fBimport()\fR an \fBunimport()\fR methods on the warnings and strict modules. .Sp For the most commonly used values, to control the handling of interpolating undefined values, the following shorthands can also be used: .Sp .Vb 3 \& NOWARN => \*(Aqunimport warnings qw(uninitialized)\*(Aq \& WARN => \*(Aq\*(Aq \& FATAL => \*(Aqimport warnings FATAL => qw(uninitialized); import strict qw(vars)\*(Aq .Ve .Sp The default state for a newly created String::Interpolate object is \&\s-1NOWARN.\s0 All other warnings are enabled as are 'refs' and 'subs' strictures. .Sp You can call \fBpragma()\fR implicitly by passing \s-1SCALAR\s0 references to \&\fBexec()\fR. Furthermore pragma('\s-1TRAP\s0') is a synonym for \fBtrap\fR\|(1) and pragma('\s-1NO TRAP\s0') is a synonym for \fBtrap\fR\|(0). Similarly for \fBlexicals()\fR, \&\fBunsafe_symbols()\fR, \fBunsafe_underscore()\fR and \fBsafe_hole()\fR. This makes the following statements equivalent: .Sp .Vb 3 \& $i\->(\e\*(AqFATAL\*(Aq,\e\*(AqNO TRAP\*(Aq,\e\*(AqSAFE SYMBOLS\*(Aq); \& $i\->pragma(\*(AqFATAL\*(Aq,\*(AqNO_TRAP\*(Aq,\*(AqNO UNSAFE_SYMBOLS\*(Aq); \& $i\->pragma(\*(AqFATAL\*(Aq)\->trap(0)\->unsafe_symbols(0); .Ve .Sp The \fBpragma()\fR method returns the object so that it can be tagged on to constructor calls. .IP "positionals" 4 .IX Item "positionals" Returns, as an lvalue, the reference to the array that holds the values to use for the positional variables \f(CW$1\fR and so on. .Sp .Vb 5 \& my @p = qw ( one two three ); \& my $i = String::Interpolate\->new( \e@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 .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" String::Interpolate::Shell, String::Interpolate::RE, String::Expand, String::MatchInterpolate. .SH "REPOSITORY" .IX Header "REPOSITORY" .SH "AUTHOR" .IX Header "AUTHOR" Created by Brian McCauley, currently maintained by Neil Bowers .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2002 by Brian McCauley. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.