| String-Gsub documentation | Contained in the String-Gsub distribution. |
String::Gsub::Matcher - match result object
use String::Gsub::Matcher; my $matcher = String::Gsub::Matcher($origstr); print $matcher; # ==> ($MATCH) print $matcher->expand($tmpl); # expand.
used by String::Gsub::Functions internally.
This module exports no functions.
| String-Gsub documentation | Contained in the String-Gsub distribution. |
## ---------------------------------------------------------------------------- # String::Gsub::Matcher # ----------------------------------------------------------------------------- # Mastering programmed by YAMASHINA Hio # # Copyright 2006 YAMASHINA Hio # ----------------------------------------------------------------------------- # $Id$ # ----------------------------------------------------------------------------- package String::Gsub::Matcher; use strict; use warnings; use overload q|""| => \&stringy; 1; # ----------------------------------------------------------------------------- # $matcher = $pkg->new($str); # sub new { my $pkg = shift; my $this = bless { origref=>\$_[0], }, $pkg; $this->{st} = [@-]; $this->{ed} = [@+]; $this; } # ----------------------------------------------------------------------------- # $str = $matcher->expand($tmpl); # evaluate string-template by match results. # sub expand { my $this = shift; my $tmpl = shift; $tmpl =~ s{\\(\d+)|\\([&`'])} { if( defined($1) ) { $1<=$#{$this->{st}} ? substr(${$this->{origref}}, $this->{st}[$1], $this->{ed}[$1] - $this->{st}[$1]) : ''; }elsif( $2 eq '&' ) { substr(${$this->{origref}}, $this->{st}[0], $this->{ed}[0] - $this->{st}[0]); }elsif( $2 eq '`' ) { substr(${$this->{origref}}, 0, $this->{st}[0]); }elsif( $2 eq "\'" ) { substr(${$this->{origref}}, $this->{ed}[0]); }else { # never reach here. ''; } }xmse; $tmpl; } # ----------------------------------------------------------------------------- # $str = $matcher->stringy(); # overloading of "". # returns whole of match ($&). # sub stringy { my $this = shift; substr(${$this->{origref}},$this->{st}[0], $this->{ed}[0]-$this->{st}[0]); } __END__
# ----------------------------------------------------------------------------- # End of File. # -----------------------------------------------------------------------------