Hi tilly,
My requirement is exactly what the attached program behaves like - only thing I did not want to use in my program was eval.
In the end, unfortunately, eval seems the only way out. I did not want it to be (I pointed out in the very beginning that eval would work but was not preferred).
Maintainance would be an issue, but then this approach would do away with people grepping through the source and locate the replacement expressions .. how useful is that too from the maintainance point of view?
With the horrific approach below, you can very well put the replacement string in an external config file and keep such things separate from the main program... in my view that is more maintainable... would you (or anyone else) not agree?
Why would
anyone keep configurable things embedded in the logic/source code?
use strict;
use warnings;
use Data::Dumper;
use Tie::File;
# open the file if it exists, but fail if it does not exist
use Fcntl 'O_RDWR';
our @array = ();
our $file = 'hai';
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) . "\"
+/>"';
my $elementIdx = 0;
while($elementIdx <= $#array)
{
if($array[$elementIdx] =~ $SearchString)
{
# Maintain only the first character
$array[$elementIdx++] = eval $ReplaceString;
}
else
{
splice @array, $elementIdx, 1;
# we don't want this in the output
}
}
untie @array; # all done!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.