Softpedia
 


LINUX CATEGORIES:



GLOBAL PAGES >>
NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
MEET THE EDITORS >>
WEEK'S BEST
  • Linux Kernel 3.9.3 / 3....
  • LibreOffice 3.6.6 / 4.0.3
  • MPlayer 1.1.1
  • systemd 204
  • Arch Linux 2013.05.01
  • Blender 2.67a
  • KDE Software Compilatio...
  • CrunchBang Linux Stable...
  • Elementary OS 0.1 / 0.2...
  • SystemRescueCd 3.6.0
  • Home > Linux > Programming > Perl Modules

    String::Interpolate 0.3

    Download button

    No screenshots available
    Downloads: 373  View global page NEW!  Tell us about an update
    User Rating:
    Rated by:
    NOT RATED
    0 user(s)
    Developer:

    License / Price:

    Last Updated:

    Category:
    Brian McCauley | More programs
    Perl Artistic License / FREE
    August 24th, 2007, 19:05 GMT
    ROOT / Programming / Perl Modules

     Read user reviews (0)  Refer to a friend  Subscribe

    String::Interpolate description

    A Perl wrapper for builtin the Perl interpolation engine.

    SYNOPSIS

    # Functional interface
    use String::Interpolate qw( safe_interpolate interpolate );
    our($GREET) = 'Hello'; # Cannot be lexical
    print interpolate( '$GREET $1n', [ 'world' ] );

    # Object interface
    use String::Interpolate;
    my $who;
    my $template = new String::Interpolate { 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 "$templaten"; # Perform interpolation

    # Peform the interpolation in a Safe compartment.
    my $replace = safe String::Interpolate 'uL$1';
    my $search = qr/(w+)/;
    $_ = "HELLO worldn";
    s/$search/$replace/eg; # /e supresses optimisation
    print;


    String::Interpolate provides a neat interface to the solution to that perenial Perl problem - how to invoke the Perl string interpolation engine on a string contained in a scalar variable.

    A String::Interpolate object encapsulates a string and a context in which it should be subjected to Perl interpolation. In the simplest, default, case the context is simply the namespace (package) from which the constructor was called.

    A String::Interpolate object may hold a reference to an array and hashes that will be used to populate the special variables $1 etc and some package variables respectively prior to each interpolation.

    In general special globally global variables such as $_ can be used in the interpolation, the exception being @_ which is always empty during the interpolation.

    The interpolated string is processed with strictures and warnings enabled excluding 'strict vars' and 'warnings uninitialized' so that interpolating undefined variables will be silently ignored. This behaviour can be altered using the pragma() method.

    Because the Perl string interpolation engine can call arbitrary Perl code you do not want to want to use it on strings from untrusted sources without some precautions. For this reason String::Interpolate objects can be made to use Safe compartments. This is, of course, only as safe as Safe and you are advised to read "WARNING" section of the Safe documentation.

    When interpolating in a Safe compartment package symbols are imported using tied wrapper variables so that their values cannot be interpreted as references and such that they cannot be used to alter the values outside the compartment. This behaviour can be suppressed by the unsafe_symbols() method. Note that if you want to import tied variable or variables containing references to objects that use overloading into a Safe compartment then you will need to do a lot of fancy footwork unless you use safe_hole() method.

    By default *_ is shared by Safe compartments and could potentially allow the compartment to leak. The $_ and %_ variables are therefore subjected to the same similar precautions to imported symbols. This behaviour can be suppressed using the unsafe_underscore() method.

    Perl string interpolation can, of course, throw exceptions. By default String::Interpolate objects do not catch (or rethrow) these exceptions when working in a simple namespace and do trap them when working in a Safe compartment. This behaviour can be overriden by the trap() or pragma() methods. If an exception during interpolation is trapped then undef will be returned as the result of the interpolation and $@ will hold the exception in the usual way.

    When taint checking enabled, attempting to perform interpolation (using eval()) on a tainted string would naturally fail. However, when using a Safe compartment, String::Interpolate will strip the tainting off of the string prior to interpolation and put it back afterwards. Also String::Interpolate will taint any arguments passed to callback functions called as the result of performing interpolation on a tainted string. Note that due to the mechanism used to assign $1 et al they can never be tained even if the values in the array being used to set them are tainted.

    By default String::Interpolate does not export any subroutines but as a concession to programmers who prefer not to explicitly use objects the functions interpolate() and safe_interpolate() are exportable.


    Product's homepage

    Requirements:

    · Perl

      


    TAGS:

    Perl wrapper | interpolation engine | Perl module | Perl | wrapper | interpolation

    Go to top

    WindowsGamesDriversMacLinuxScriptsMobileHandheldNews

    SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   UPDATE YOUR SOFTWARE   |   ROMANIAN FORUM