Hi monks!
I'm thinking of writing a Perl class for personal use, but would like to check first whether a similar module already exists in which case I could just use that. I failed to find anything using CPAN search or Google (I can't really come up with any good search terms that express the concept in few words), so I'm seeking your help.
The idea is to have substrings that are linked to the original text they are a substring of, and keep track of their exact start and end position within it. When the substring is modified, the change automatically propagates to the original text. The substrings could themselves have linked substrings extracted from them, and so on.
Here's a simple example of how it could be used (in this case, to replace {{...}} template markers in the third subsection of a text document with appropriate replacement text):
my $document = new My::String( read_file('doc.txt') ); my @sections = $document->split( qr/^------\n/ ); my @markers = $sections[2]->match( qr/{{\w+}}/ ); foreach $marker (@markers) { # $marker will be a My::String object my $old = $marker->text(); my $new = ''; # ... # Complex code to generate replacement text, depending on the # exact contents of $old, goes here # ... $marker->setText( $new ); # change propagates to $document } write_file( 'doc.txt', $document->text() );
There may be a better way to design the API, but I hope you get the general idea.
Now, I fully realize that the above example could be implemented using s/.../.../e regex substitution (i.e. with Perl code as the replacement pattern). In fact that's how I've done this kind of thing in the past. But it would be messy, even with this simple example.
For more complex examples of dynamic text substitution, with more layers of substrings-within-substrings that need special-case handling, using s/.../.../e regexes really doesn't scale too well in terms of code tidiness and maintainability, and an object-oriented framework of linked substrings like I laid out above might prove to be a better solution.
So, I'm looking for your input...
Does such a module already exist? What might it be called? What do you think of the general idea?
Thanks!
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |