Agreed. Combining the ability to call a subroute to do your bidding and back references makes for some very handy abilities.
For example:
Say I have a string:

"The %NOUN% can jump very %ADVERB%"

And say I have hash:
%wordBank = ( NOUN => "kangaroo", ADVERB => "far", )

I wanted to replace what is in the string with what is in the hash, so originally, I iterated through the hash and did a replace on the string as I found each hash key. This is terribly inefficient, so I reworked it to find items delimited by %..% in the string, and used the delimited keyword to directly call the value from the hash. This save me a lot of time, especially when %wordBank is very large.
#!/usr/bin/perl -w use strict; sub safeReplace { my($hashkey,%hash) = @_; my $hash = \%hash; my $retval = ''; if (defined($hash{$hashkey})) { $retval = $hash{$hashkey}; } else { $retval = "\%$hashkey\%"; } return $retval; } sub preProcessStr { my($string,%options) = @_; $string =~ s/%(.+?)%/safeReplace($1,%options)/ge; return $string; } my %options = ( ARG1 => "test", ARG2 => "123...", ); my $string = 'Hello %ARG1%, counting down %ARG2% .. %NOT%'; $string = preProcessStr($string,%options); print "$string\n"

In reply to Re: Re: how do I use s///e by perlfan
in thread how do I use s///e by PastisALo

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.