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"; }