I have a regex search expression, and then a replacement expression.
My requirement is to have function call invocations embedded in the replacement expression that would be kept separate from the logic - say at the beginning of the script, and I want the replacement expression to be evaluated when the search and replacement is done - and not when the replacement expression is initialized to the variable that contains it.

And now some code of (what I want to do in comments) what I have:
use strict; use warnings; use Data::Dumper; use Tie::File; # open the file if it exists, but fail if it does not use Fcntl 'O_RDWR'; our @array = (); our $file = 'taste'; tie @array, 'Tie::File', $file, mode => O_RDWR or die $!; our $SearchString = 'HAI WORLD Times ([0-9]+).*$'; #our $ReplaceString = "\t\t<exML LOLZ = \"" . unpack('a1', $1) . '"/>' +; # we could use such a statement ONLY if we figured out what somethi +ng() would be my $elementIdx = 0; while($elementIdx <= $#array) { if($array[$elementIdx] =~ $SearchString) { # Maintain only the first character #$array[$elementIdx] = unpack ('a' . (1 + length("\t\t<exML LO +LZ = \"\"/>")), something($ReplaceString)); # I would love to know wh +at something() could be so that I can separate the configuration from + the logic $array[$elementIdx++] = "\t\t<exML LOLZ = \"" . unpack('a1', $ +1) . '"/>'; #++$elementIdx; } else { splice @array, $elementIdx, 1; # we don't want this in the output } } untie @array; # all done!

Sample input file:
HAI WORLD Times 0 HAI WORLD Times 1 HAI WORLD Times 2 HAI WORLD Times 3 HAI WORLD Times 4 HAI WORLD Times 5 HAI WORLD Times 6 HAI WORLD Times 7 HAI WORLD Times 8 HAI WORLD Times 9 HAI WORLD Times 10 HAI WORLD Times 11 HAI WORLD Times 12 HAI WORLD Times 13 HAI WORLD Times 14 HAI WORLD Times 15 HAI WORLD Times 16 HAI WORLD Times 17 HAI WORLD Times 18 HAI WORLD Times 19 HAI WORLD Times 20 HAI WORLD Times 21 HAI WORLD Times 22 HAI WORLD Times 23 HAI WORLD Times 24 HAI WORLD Times 25

Corresponding output file:
<exML LOLZ = "0"/> <exML LOLZ = "1"/> <exML LOLZ = "2"/> <exML LOLZ = "3"/> <exML LOLZ = "4"/> <exML LOLZ = "5"/> <exML LOLZ = "6"/> <exML LOLZ = "7"/> <exML LOLZ = "8"/> <exML LOLZ = "9"/> <exML LOLZ = "1"/> <exML LOLZ = "1"/> <exML LOLZ = "1"/> <exML LOLZ = "1"/> <exML LOLZ = "1"/> <exML LOLZ = "1"/> <exML LOLZ = "1"/> <exML LOLZ = "1"/> <exML LOLZ = "1"/> <exML LOLZ = "1"/> <exML LOLZ = "2"/> <exML LOLZ = "2"/> <exML LOLZ = "2"/> <exML LOLZ = "2"/> <exML LOLZ = "2"/> <exML LOLZ = "2"/>

What would something() look like?
Update : Unfortunately, something looks like eval... oh well...

In reply to Function call in regex replacement string by PoorLuzer

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.