I am currently using this code to replace $a and $b variables from a template. However I need to make this more dynamic so I do not need to specify the values any more.
sub prog_replace_string { my $replace_this = shift; my $with_this = shift; my $string = shift; my $length = length($string); my $target = length($replace_this); for(my $i=0; $i<$length - $target + 1; $i++) { if(substr($string,$i,$target) eq $replace_this) { $string = substr($string,0,$i) . $with_this . substr($stri +ng,$i+$target); } } return $string; }
I was wondering how to do replace certain denoted names dynamically in a template file, such as in this context: <b><u><i>$var</i></u></b> and replacing this with the value of $var. I would think this would be possible by using a reference to the regexp match in a regexp like: $string = s/\$XXXXXXXX/${$1}/; Of course this does not work, I do not know what to replace XXXXXXXX with and the $1 variable would return the entire variable name, however we need it without the $ sign. This must be really easy. I would love if someone could help me out without pointing to the use of a module. Thanks a lot!

In reply to Replacing string with regexp without using modules by cowgirl

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.