Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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?

In reply to Is there a module for object-oriented substring handling/substitution? by smls

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (6)
    As of 2015-11-25 07:06 GMT
    Find Nodes?
      Voting Booth?

      What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

      Results (672 votes), past polls