Ovid's recent question regarding Prematch, Postmatch, and Match special variables ($`, $', and $&), and their associated global performance hit reminded me that this was something I played with a few months ago. Once I found a solution I stopped looking.

Having just read merlyn's Alpaca book, and thus being in "Object Oriented" mode when I read Ovid's question, I started thinking of an OOP alternative to using $`, $', and $&.

In this solution I tie three scalars to behave just like the infamous special variables, but without the global performance hit. Unfortunately, the string being matched against must be passed by reference to the objects as the scalars are being tied. For that reason, this solution feels a little kludgy to me, but I thought it was interesting enough to post anyway.

I haven't made any attempt to guard against undefined @- and @+ in the case of unsuccessful matches, and it may go to hell in a handbasket for s/// where the string changes length. But it's still kinda fun to play with. Enjoy!

Dave

package Match; use strict; use warnings; sub TIESCALAR { my ( $class, $r_string ) = @_; my $self = {}; $self->{String_Ref} = $r_string; $self->{Value} = undef; bless $self, $class; } sub STORE { my ( $self, $value ) = @_; $self->{Value} = $value; return $self->{Value}; } sub FETCH { my $self = shift; $self->{Value} = substr( ${$self->{String_Ref}}, $-[0], $+[0] - $-[0] ); return $self->{Value}; } sub DESTROY { my $self = shift; } 1; package Prematch; use base "Match"; sub FETCH { my $self = shift; $self->{Value} = substr( ${$self->{String_Ref}}, 0, $-[0] ); return $self->{Value}; } 1; package Postmatch; use base "Match"; sub FETCH { my $self = shift; $self->{Value} = substr( ${$self->{String_Ref}}, $+[0] ); return $self->{Value}; } 1; # ---------- Begin main ---------- package main; use strict; use warnings; my ( $match, $pre, $post ); my $string = "This is a contrived test string..."; tie $match, "Match", \$string; tie $pre, "Prematch", \$string; tie $post, "Postmatch", \$string; if ( $string =~ /contrived/) { print $pre,"\n"; print $match, "\n"; print $post, "\n"; }

In reply to Tied scalars to emulate $&, $' and $` without global performance hit by davido

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.