String-Interpolate-0.33/000755 000765 000024 00000000000 14027212061 015420 5ustar00neilbstaff000000 000000 String-Interpolate-0.33/LICENSE000644 000765 000024 00000043663 14027212061 016441 0ustar00neilbstaff000000 000000 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. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2002 by Brian McCauley. This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2002 by Brian McCauley. This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End String-Interpolate-0.33/Changes000644 000765 000024 00000001730 14027212061 016714 0ustar00neilbstaff000000 000000 Revision history for Perl module String::Interpolate 0.33 2021-03-25 NEILB - Fixed typos in doc and comments. Thanks to Gregor Herrmann, RT#129224 0.32 2015-06-28 NEILB - Added SEE ALSO section. SWEETKID++ - Got rid of indirect method notation in the doc. SWEETKID++ 0.31 2015-06-07 NEILB - Switched to Dist::Zilla - Fixed RT#95320 - removed space character from function prototype - Added copyright and author section - Fixed 2 minor pod errors - Reformatted this file as per CPAN::Changes::Spec 0.3 2004-05-07 NOBULL - suppress error in exec() when $_ undefined in safe_underscore mode - propagate arguments from safe() constructor to new() constructor - Safe::Hole in opcode-restoring mode if $Safe::Hole::VERSION > 0.09 - Spelling corrections in POD - Thanks to Paul Kulchenko for bug report 0.2 2003-11-14 NOBULL - bugfix in exec - experimental support for lexicals 0.1 2002-04-17 NOBULL - original version; String-Interpolate-0.33/MANIFEST000644 000765 000024 00000000303 14027212061 016545 0ustar00neilbstaff000000 000000 # This file was automatically generated by Dist::Zilla::Plugin::Manifest v6.012. Changes LICENSE MANIFEST META.json META.yml Makefile.PL README dist.ini lib/String/Interpolate.pm t/interpolate.t String-Interpolate-0.33/t/000755 000765 000024 00000000000 14027212061 015663 5ustar00neilbstaff000000 000000 String-Interpolate-0.33/README000644 000765 000024 00000000607 14027212061 016303 0ustar00neilbstaff000000 000000 This archive contains the distribution String-Interpolate, version 0.33: Wrapper for builtin the Perl interpolation engine. 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. This README file was generated by Dist::Zilla::Plugin::Readme v6.012. String-Interpolate-0.33/META.yml000644 000765 000024 00000001450 14027212061 016671 0ustar00neilbstaff000000 000000 --- abstract: 'Wrapper for builtin the Perl interpolation engine.' author: - 'Brian McCauley ' build_requires: {} configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 0 generated_by: 'Dist::Zilla version 6.012, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: String-Interpolate requires: Carp: '0' Exporter: '0' PadWalker: '0' Safe: '0' Safe::Hole: '0' Symbol: '0' base: '0' overload: '0' perl: '5.006' strict: '0' warnings: '0' resources: homepage: https://github.com/neilb/String-Interpolate repository: https://github.com/neilb/String-Interpolate.git version: '0.33' x_generated_by_perl: v5.28.2 x_serialization_backend: 'YAML::Tiny version 1.73' String-Interpolate-0.33/lib/000755 000765 000024 00000000000 14027212061 016166 5ustar00neilbstaff000000 000000 String-Interpolate-0.33/Makefile.PL000644 000765 000024 00000002452 14027212061 017375 0ustar00neilbstaff000000 000000 # This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v6.012. use strict; use warnings; use 5.006; use ExtUtils::MakeMaker; my %WriteMakefileArgs = ( "ABSTRACT" => "Wrapper for builtin the Perl interpolation engine.", "AUTHOR" => "Brian McCauley ", "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => 0 }, "DISTNAME" => "String-Interpolate", "LICENSE" => "perl", "MIN_PERL_VERSION" => "5.006", "NAME" => "String::Interpolate", "PREREQ_PM" => { "Carp" => 0, "Exporter" => 0, "PadWalker" => 0, "Safe" => 0, "Safe::Hole" => 0, "Symbol" => 0, "base" => 0, "overload" => 0, "strict" => 0, "warnings" => 0 }, "VERSION" => "0.33", "test" => { "TESTS" => "t/*.t" } ); my %FallbackPrereqs = ( "Carp" => 0, "Exporter" => 0, "PadWalker" => 0, "Safe" => 0, "Safe::Hole" => 0, "Symbol" => 0, "base" => 0, "overload" => 0, "strict" => 0, "warnings" => 0 ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) { delete $WriteMakefileArgs{TEST_REQUIRES}; delete $WriteMakefileArgs{BUILD_REQUIRES}; $WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs; } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); String-Interpolate-0.33/META.json000644 000765 000024 00000002540 14027212061 017042 0ustar00neilbstaff000000 000000 { "abstract" : "Wrapper for builtin the Perl interpolation engine.", "author" : [ "Brian McCauley " ], "dynamic_config" : 0, "generated_by" : "Dist::Zilla version 6.012, CPAN::Meta::Converter version 2.150010", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "String-Interpolate", "prereqs" : { "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "Carp" : "0", "Exporter" : "0", "PadWalker" : "0", "Safe" : "0", "Safe::Hole" : "0", "Symbol" : "0", "base" : "0", "overload" : "0", "perl" : "5.006", "strict" : "0", "warnings" : "0" } } }, "release_status" : "stable", "resources" : { "homepage" : "https://github.com/neilb/String-Interpolate", "repository" : { "type" : "git", "url" : "https://github.com/neilb/String-Interpolate.git", "web" : "https://github.com/neilb/String-Interpolate" } }, "version" : "0.33", "x_generated_by_perl" : "v5.28.2", "x_serialization_backend" : "Cpanel::JSON::XS version 4.24" } String-Interpolate-0.33/dist.ini000644 000765 000024 00000000525 14027212061 017066 0ustar00neilbstaff000000 000000 name = String-Interpolate author = Brian McCauley license = Perl_5 copyright_holder = Brian McCauley copyright_year = 2002 main_module = lib/String/Interpolate.pm version = 0.33 [@Basic] [PkgVersion] [AutoPrereqs] [MetaJSON] [GithubMeta] [Git::Tag] tag_message= [Git::Push] String-Interpolate-0.33/lib/String/000755 000765 000024 00000000000 14027212061 017434 5ustar00neilbstaff000000 000000 String-Interpolate-0.33/lib/String/Interpolate.pm000644 000765 000024 00000077676 14027212061 022307 0ustar00neilbstaff000000 000000 package String::Interpolate; $String::Interpolate::VERSION = '0.33'; use 5.006; use strict; use warnings; use Carp qw( croak ); =head1 NAME String::Interpolate - Wrapper for builtin the Perl interpolation engine. =head1 SYNOPSIS # 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; =head1 DESCRIPTION C 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 C 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 C 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 C objects can be made to use C 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 C 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. =cut # Must appear before any file-scoped lexicals sub reval { no strict 'vars'; eval $_[0] } sub prevent_blessed_error_hack () { return unless ref $@; no strict 'refs'; no warnings 'redefine'; local *{"@{[ref $@]}::DESTROY"} = sub {}; $@ = 'Blessed error from Safe compartment'; } # During Carp::confess stack dumps we don't want to exec() # %dbgpkg is a package variable as callers may want to manipulate it. our %dbgpkg = ( Carp => 1, ); our $taint_flag = ''; our $safe_hole; my %type_from_prefix = ( "\$" => 'SCALAR', '@' => 'ARRAY', '%' => 'HASH', ); use overload '""' => sub { my $self = shift; $dbgpkg{caller()} ? overload::StrVal($self) : $self->exec; }, 'cmp' => sub { my ($l,$r) = @_; $l->exec cmp $r }, '@{}' => sub { tie my @a, 'String::Interpolate::AsArray', @_; \@a }, '%{}' => 'ashash', '&{}' => sub { my $self=shift; sub { $self->exec(@_) } }; use base 'Exporter'; our(@EXPORT_OK) = qw ( interpolate safe_interpolate ); my $pkgcount; =head2 Principle methods =over 4 =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 exec() method. Returns a the object. 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. =cut my %preset_pragma = ( NOWARN => 'unimport warnings qw(uninitialized)', WARN => '', FATAL => 'import warnings FATAL => qw(uninitialized); import strict qw(vars)', ); sub new { my $class = shift; my $self; if ( ref $class ) { # Clone $self = bless \ { %$$class }, ref $class; delete @$$self{'tmppkg','pkg','code'} if $$self->{tmppkg}; delete $$self->{safe} if $$self->{implicit_safe}; } else { my $calldepth = 0; my $defpgk; do { $defpgk = caller($calldepth++) } while $defpgk->isa( __PACKAGE__ ); $self = bless \ { defpgk => $defpgk, pkg => $defpgk, pragmas => $preset_pragma{NOWARN}, }, $class; } $self->exec(@_); $self; } =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 DESTROY 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. 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. =cut sub safe { my $self = shift; $self = $self->new(@_) unless ref $self; $self->free_tmppkg; delete @$$self{'pkg','explicit_pkg','safe'}; $$self->{implicit_safe}++; require Safe; $self; } =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 CODE reference context. The following are equivalent pairs: 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. =over 4 =item ARRAY reference Tells the object to use this array to populate the special variables $1 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 ARRAY reference is exposed via the positionals() method. The array can also be modified by using the String::Interpolate object in an ARRAY reference context. Note, however, that the String::Interpolate object used in an ARRAY reference context does not refer to the array itself but to a STORE-only tied array whose subscripts are offset by one such that $interpolate_object->[1] corresponds to $interpolate_object->positionals->[0] and hence the value that will be interpolated for $1. =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. 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: =over 4 =item ARRAY reference Set the symbol table entry's ARRAY slot. =item HASH reference Set the symbol table entry's HASH slot. =item SCALAR reference Set the symbol table entry's SCALAR slot. =item CODE reference with prototype () Set the symbol table entry's SCALAR slot to point to an new tied scalar with a FETCH method that calls the referenced code. 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. =item CODE reference with prototype ($) or no prototype Set the symbol table entry's HASH slot to point to an new tied hash with a FETCH method that calls the referenced code. 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. =item Anything else Set the symbol table entry's SCALAR slot to point scalar containing the value. =back 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 =item GLOB or GLOB reference Instruct the object to perform interpolation in the namespace defined by the GLOB. 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. 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. =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 safe() 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. =item Safe::Hole object Equivalent to calling the safe_hole() method with the same argument. =item SCALAR reference The referenced scalar is passed to the pragma() method. =item Anything else Use the stringified value of the argument as the string on which to perform interpolation. =back =cut sub exec { my $self = shift; $self = $self->new unless ref $self; my $seenmap; for ( @_ ) { if ( ref eq 'ARRAY' ) { $$self->{pos} = $_; } elsif ( ref eq 'HASH' ) { my $map = \$$self->{map}; if ( !$seenmap++ && $$map && @$$map ){ $$map = []; $self->free_tmppkg; } push @$$map => $_; } elsif ( ref $_ eq 'SCALAR' ) { $self->pragma($$_); } elsif ( ref $_ eq 'GLOB' || ref \$_ eq 'GLOB' ) { $self->package($_); } elsif ( ref && $_->isa('Safe::Hole') ) { $$self->{safe_hole} = $_; } elsif ( ref && $_->isa('Safe') ) { $self->free_tmppkg; delete $$self->{pkg}; delete $$self->{implicit_safe}; delete $$self->{lexicals}; $$self->{safe} = $_; $$self->{trap} = 1 unless defined $$self->{trap}; } else { $$self->{string} = "$_"; delete $$self->{code}; } } return unless defined wantarray; @_ = (); local $_ = $_; my $string = $$self->{string}; my $pos = $$self->{pos}; my $pkg = $$self->{pkg}; my $safe = $$self->{safe}; my $code = $$self->{code}; if ( $$self->{implicit_safe} && !$safe ) { $safe = $$self->{safe} = Safe->new; $safe->deny('tie','bless'); } my $dlm = '_aaa'; if ( defined $string && !$code || $pos ) { my $cat = join '' => $string, @{ $pos || [] }; $dlm++ while -1 < index $cat, $dlm; } ( join $dlm => @$pos ) =~ /^@{[ join $dlm => ('(.*)') x @$pos ]}$/ or die 'Unexpected pattern match failure initialising $1 et al' if $pos; if ( $pkg && $pkg eq 'Safe') { require Safe; $safe = Safe->new; } $pkg = $safe->root if $safe; local $_ = do { no warnings 'uninitialized'; "$_"}, local *_ = %_ ? String::Interpolate::Func->wrap_hash('_',\%_) : {} if $safe && ! $$self->{unsafe_underscore}; my $safe_symbols = $safe && ! $$self->{unsafe_symbols}; # use PadWalker qw( peek_my ); use Data::Dumper; die Dumper peek_my(2); my @pad_map; if ( $$self->{lexicals} ) { my $depth = 1; $depth++ while caller($depth)->isa(__PACKAGE__); # die "$depth ". scalar(caller($depth)); require PadWalker; my $pad = PadWalker::peek_my($depth+1); # use Data::Dumper; die Dumper $pad; while ( my ( $k,$v ) = each %$pad ) { $k =~ s/^([@%\$])// or die "$k does not start with \$, \@ or \%"; $v = *$v{$type_from_prefix{$1}} if ref $v eq 'GLOB'; push @pad_map => { $k => $v }; } } for ( @pad_map, @{$$self->{map}} ) { $pkg ||= $$self->{tmppkg} ||= __PACKAGE__ . '::' . ++$pkgcount; while ( my ( $k,$v ) = each %$_ ) { no strict 'refs'; *{"${pkg}::$k"} = do { if ( ref $v eq 'HASH' ) { if ( $safe_symbols ) { String::Interpolate::Func->wrap_hash($k,$v); } else { $v; } } elsif ( ref $v eq 'CODE' ) { my $p = prototype($v); if ( defined $p && ! $p ) { my $unimplemented = sub { croak "\$$k tied scalar is FETCH-only within String::Interpolate"; }; tie my $s, 'String::Interpolate::Func', { FETCH => $v, STORE => $unimplemented, }; \$s; } elsif ( $p && $p ne "\$" ) { croak "Invalid prototype ($p) for interpolated function $k"; } else { my $unimplemented = sub { die "%$k tied hash is FETCH-only within String::Interpolate"; }; tie my %h, 'String::Interpolate::Func', { FETCH => $v, STORE => $unimplemented, DELETE => $unimplemented, FIRSTKEY => $unimplemented, NEXTKEY => $unimplemented, }; \%h; } } elsif ( ref $v eq 'ARRAY' ) { if ( $safe_symbols ) { my $unimplemented = sub { die "\@$k is read-only within String::Interpolate"; }; tie my @a, 'String::Interpolate::Func', { FETCH => sub { "$v->[$_[0]]" }, STORE => $unimplemented, DELETE => $unimplemented, FETCHSIZE => sub { scalar @$v }, }; \@a; } else { $v; } } elsif ( ref $v eq 'SCALAR' ) { if ( $safe_symbols ) { my $unimplemented = sub { die "\$$k is read-only within String::Interpolate"; }; tie my $s, 'String::Interpolate::Func', { FETCH => sub { "$$v" }, STORE => $unimplemented, }; \$s; } else { $v; } } else { if ( $safe_symbols ) { \ "$v"; } else { \$v; } } }; } } unless ( $code ) { unless ( defined $string ) { croak("No string to interpolate"); } $string = "BEGIN{import strict qw(refs subs); $$self->{pragmas}}; sub{<<$dlm\n$string\n$dlm\n}"; if ( $safe ) { no strict 'refs'; for ( 'String::Interpolate::Func::AUTOLOAD', 'warnings::unimport', 'warnings::import', 'strict::unimport', 'strict::import' ) { *{"${pkg}::$_"} = \&$_; } # Remove taint and generate a poor man's Safe::Hole no warnings 'redefine'; *{"${pkg}::String::Interpolate::code"} = $safe->reval( $string =~ /(.*)/s ); $code = 1; # Just a flag in this case # prevent_blessed_error_hack; } else { $pkg ||= $$self->{defpgk}; $code = reval "package $pkg; $string"; } if ( $@ ) { return if $$self->{trap}; croak( $@ ); } $$self->{code} = $code; }; # Restore taint by appending null cut from $string if ( $safe ) { local $taint_flag = substr( $string, 0, 0 ); local $safe_hole = $$self->{safe_hole}; $string = $safe->reval('&String::Interpolate::code'); # prevent_blessed_error_hack; if ( $@ ) { return if $$self->{trap}; croak( $@ ); } } else { $string = $$self->{trap} ? eval { &$code } : &$code; } chop $string; # If we copied the lexicals then we must clean house to # avoid keeping them spuriously alive. $self->free_tmppkg if $$self->{lexicals}; $string; } =back =head2 Functional interface For those heathens who don't like the OO interface. =over 4 =item safe_interpolate Exportable function equivalent to String::Interpolate->safe->exec(LIST). =cut sub safe_interpolate { __PACKAGE__->safe->exec(@_); } =item interpolate Exportable function equivalent to String::Interpolate->lexicals->exec(LIST). =cut sub interpolate { __PACKAGE__->lexicals->exec(@_); } =back =head2 Ancillary methods The following methods provide alternative interfaces and some fine tuning capabilities. =over 4 =item trap Tells the String::Interpolate object whether or not to trap exceptions. $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. =cut sub trap { my $self = shift; $self = $self->new unless ref $self; my $trap = shift; $$self->{trap} = defined $trap ? $trap : 1; $self; } =item unsafe_underscore Tells the String::Interpolate object whether or not to use "unsafe underscore" mode. In this mode no precautions are taken to prevent malicious code attempting to reach outside it's Safe compartment through the $_ and %_ variables. $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. =cut sub unsafe_underscore { my $self = shift; $self = $self->new unless ref $self; my $unsafe_underscore = shift; $$self->{unsafe_underscore} = defined $unsafe_underscore ? $unsafe_underscore : 1; $self; } =item unsafe_symbols Tells the String::Interpolate object whether or not to use "unsafe symbol" 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. $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. =cut sub unsafe_symbols { my $self = shift; $self = $self->new unless ref $self; my $unsafe_symbols = shift; $$self->{unsafe_symbols} = defined $unsafe_symbols ? $unsafe_symbols : 1; $self; } =item lexicals This feature is EXPERIMENTAL. Do not use it in real code. 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. =cut sub lexicals { my $self = shift; $self = $self->new unless ref $self; my $lexicals = shift; if ( ( $$self->{lexicals} = defined $lexicals ? $lexicals : 1 ) ) { delete $$self->{pkg}; delete $$self->{safe}; } $self; } =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 GLOB or a GLOB reference. The trailing '::' may be omitted. With an undefined argument this method instructs the object to use a new automatically allocated temporary namespace. 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. =cut sub package { my $self = shift; $self = $self->new unless ref $self; my $pkg = shift; $pkg = *$pkg if ref $pkg eq 'GLOB'; ($pkg) = $pkg =~ /^\*?(?:main::(?!$))*(.*?)(?:::)?$/ or die; $self->free_tmppkg; delete $$self->{safe}; delete $$self->{implicit_safe}; delete $$self->{lexicals}; $$self->{pkg} = $$self->{explicit_pkg} = $pkg; $self; } =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 eval(), symbolic references and method calls in callbacks won't function normally. 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. =cut sub safe_hole { my $self = shift; $self = $self->new unless ref $self; my $safe_hole = shift; unless ( UNIVERSAL::isa( $safe_hole, 'Safe::Hole' )) { if ( $safe_hole || !defined $safe_hole ) { unless ( eval { require Safe::Hole; 1 } ) { require Carp; Carp::croak('String::Interpolate::safe_hole() requires Safe::Hole module'); } $safe_hole = Safe::Hole->new(($Safe::Hole::VERSION > 0.09) ? ({}) : ()); } else { undef $safe_hole; } } $$self->{safe_hole} = $safe_hole; $self; } =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 import() an unimport() methods on the warnings and strict modules. 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. =cut sub pragma { my $self = shift; $self = $self->new unless ref $self; for my $pragma ( @_ ) { my ( $no, $method, $un) = $pragma =~ /^(NO[ _]?)?(LEXICALS|TRAP|SAFE[_ ]HOLE|(?:((?:UN)?)SAFE[_ ](?:SYMBOLS|UNDERSCORE)))$/; if ( $method ) { # For methods that start 'un' but for which the 'un' has been omitted # reinstate the un and invert the sense of the 'no' prefix. if ( defined $un && !$un ) { $no = !$no; $method = "UN$method"; } $method =~ tr/ A-Z/_a-z/; $self->$method(!$no + 0); } else { $$self->{pragma} = $preset_pragma{$pragma} || $pragma; } } $self; } sub DESTROY { shift->free_tmppkg; } sub free_tmppkg { my $self = shift; delete $$self->{code}; delete $$self->{safe} if $$self->{implicit_safe}; if ( $$self->{tmppkg} ) { require Symbol; Symbol::delete_package( delete $$self->{tmppkg} ); } } =item positionals Returns, as an lvalue, the reference to the array that holds the values to use for the positional variables $1 and so on. 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 =cut sub positionals : lvalue { my $self = shift; $$self->{pos}; } sub ashash { my $self = shift; $self->exec({}) unless $$self->{map}; $$self->{map}[-1]; } package String::Interpolate::AsArray; $String::Interpolate::AsArray::VERSION = '0.33'; sub TIEARRAY { my ($class, $thing ) = @_; bless \$thing, $class } sub STORE { ${${$_[0]}}->{pos}[$_[1]-1]=$_[2] } sub FETCH { require Carp; Carp::croak('String::Interpolate objects STORE-only in ARRAY context'); } *FETCHSIZE = \&FETCH; # A private and very secretive class to give secure access to an object package String::Interpolate::Func; $String::Interpolate::Func::VERSION = '0.33'; sub wrap_hash { my $class = shift; my ($k,$v) = @_; my $unimplemented = sub { die "%$k is read-only within String::Interpolate"; }; tie my %h, $class, { FETCH => sub { "$v->{$_[0]}" }, STORE => $unimplemented, DELETE => $unimplemented, FIRSTKEY => sub { keys %$v; each %$v }, NEXTKEY => sub { each %$v }, }; \%h; } sub TIEARRAY { my $actions = $_[1]; bless sub { return unless my $action = $actions->{+shift}; # Launder the argument list in case $action is wrapped by Safe::Hole # If the interpolated string was tainted then so are any arguments # passed from it. @_ = map { "$taint_flag$_" } @_; goto &$action unless $safe_hole; $safe_hole->call($action,@_); }, $_[0]; } *TIEHASH = \&TIEARRAY; *TIESCALAR = \&TIEARRAY; sub AUTOLOAD { my $self = shift; unshift @_ => our($AUTOLOAD) =~ /(\w+)$/; goto &$self; } 1; __END__ =back =head1 SEE ALSO L, L, L, L. =head1 REPOSITORY L =head1 AUTHOR Created by Brian McCauley, currently maintained by Neil Bowers Eneilb@cpan.orgE =head1 COPYRIGHT AND LICENSE 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. =cut String-Interpolate-0.33/t/interpolate.t000644 000765 000024 00000003565 14027212061 020407 0ustar00neilbstaff000000 000000 #!./perl use strict; use warnings; use String::Interpolate; print "1..28\n"; my $testno; sub t ($) { print "not " unless shift; print "ok ",++$testno,"\n"; } my $i = String::Interpolate->new; 'DOL1,DOL2' =~ /(.*),(.*)/; local $_ = 'US'; local %_ = ( R => '_R' ); our($A) = 'A'; our(@A) = ( 'A0', 'A1' ); our(%B) = ( X => 'BX', Y => 'BY' ); keys %B; t( $i->('$_ $_{R} $1 $2 $A $A[0] $A[1] $B{X} $B{Y}\n') eq "US _R DOL1 DOL2 A A0 A1 BX BY\n"); $i->( { a => \$A, b => 'B' }, { a => \@A, b => \%B } ); t($i->exec('$_ $_{R} $1 $2 $a @a $a[0] $b{X} $b') eq "US _R DOL1 DOL2 A A0 A1 A0 BX B"); $i->{b}{C} = 'bc'; t( $B{C} eq 'bc'); $i->{REV} = sub ($) { reverse @_ }; $i->{LC} = sub { lc shift }; $i->{L} = sub () { 'lit' }; $i->[1] = 'd1'; $A = 'aa'; t($i->exec('$a $REV{FOO} $LC{BAR} $L $1 $2 $::A $b{C}') eq "aa OOF bar lit d1 aa bc"); t(@{$i->positionals} == 1 && $i->positionals->[0] eq 'd1'); $i->positionals->[1] = 'd2'; t("$i" eq "aa OOF bar lit d1 d2 aa bc"); my @p = ('D1'); $i->positionals = \@p; $i->[2] = 'D2'; t($p[1] eq 'D2'); $i->safe; t($i eq 'aa OOF bar lit D1 D2 bc'); t($i->({ Z => 1 },'$Z $a') eq '1 '); undef $i->positionals; t($i->('$1') eq 'DOL1'); # Test the various ways of specifying package no warnings 'once'; t($i->(*FOO1,'@{[ __PACKAGE__ ]}') eq 'FOO1'); t($i->(\*FOO2) eq 'FOO2'); t($i->(*FOO3::) eq 'FOO3'); t($i->package('FOO4')->() eq 'FOO4'); # Test the various ways of specifying pragmas for ( 'symbols', 'underscore' ) { my $method = "unsafe_$_"; t( ! defined $$i->{$method} ); $i->$method; t( $$i->{$method} ); $i->$method(0); t( ! $$i->{$method} ); $i->$method(1); t( $$i->{$method} ); $i->(\ "SAFE \U$_"); t( ! $$i->{$method} ); $i->pragma("\U$method"); t( $$i->{$method} ); $i->pragma("NO UNSAFE \U$_"); t( ! $$i->{$method} ); }