Thanks Ken,

Your solution is a lot like what I've come up with but I've taken it a bit further. I checked out the modules Anonymous suggested. String::Interpolate::RE served as the inspiration for my solution.

My solution handles \\\w escapes, \\\w{\w+} and ${\w+} interpolation now where ${\w+} is simply checked against %ENV for a value.

The hash keys include the backslash in the case of \\\w and \\\w{\w+} and the values can be either scalars or coderefs.

I have included the $opts hashref but have not implemented it yet. I will in my final project probably use $opts much like String::Interpolate::RE.

#!/usr/bin/perl use strict; use Cwd; use POSIX; use Sys::Hostname; use File::Basename; sub interpolate { # heavily borrowed from http://search.cpan.org/~djerius/String-Inter +polate-RE-0.03/lib/String/Interpolate/RE.pm my ($string, $repls, $opts) = (@_); $string =~ s{ ( (?:(\\.)(?:\{([^\}]+)\})?)| (?:\${(\w+)}) ) }{ my $v; my $t = (defined($2)) ? $2 : $4; if (exists($repls->{$t})) { $v = ref($repls->{$t}) eq 'CODE' ? &{$repls->{$t}}($t,$3) : $rep +ls->{$t} } elsif (exists($ENV{$t})) { $v = $ENV{$t} } defined($v) ? $v : $1 }egx; return $string; } my $string = '\s\n\u@\h \w \D{%F}\n\t\n\T\n\@\n\A\n\$\n${LANG}\n\q\n${ +FOO} $ '; my %repls = ( '\a' => "\a", '\D' => sub { strftime($_[1], localtime) }, '\e' => "\029", '\h' => sub { (split(/\./,hostname()))[0] }, '\H' => sub { hostname() }, '\n' => "\n", '\r' => "\r", '\s' => sub { basename($0) }, '\t' => sub { strftime('%T', localtime) }, '\T' => sub { strftime('%I:%M:%S', localtime) }, '\A' => sub { strftime('%R', localtime) }, '\@' => sub { strftime('%I:%M %p', localtime) }, '\u' => sub { getpwuid($>) }, '\w' => sub { $_ = cwd(); s/$ENV{HOME}/~/ ; return $_; }, '\W' => sub { (cwd() eq $ENV{HOME}) ? '~' : basename(cwd()) }, '\$' => $>, ); print interpolate($string,\%repls) . "\n";

Gives:

alan@host ~/test/prompt 2013-04-11
19:43:45
07:43:45
07:43 PM
19:43
500
en_US.utf8
\q
${FOO} $ '

Adding and removing escapes is a matter of adding or removing keys from the hash. Which is good because I am writing a shell framework. (Yes I know about Term::Shell and others but they weren't quite what I want.) But I can include the sort of standard escapes with the Shell and then with my specific implementation of it I can add more...

Thanks Ken, thanks Monks.

-Alan


In reply to Re^2: replace escape sequences in a string by alanwevans
in thread replace escape sequences in a string by alanwevans

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.