in reply to Why can't $` $& $' be enabled on a per-regex basis?
So:$` is the same as "substr($var, 0, $-[0])" $& is the same as "substr($var, $-[0], $+[0] - $-[0])" $' is the same as "substr($var, $+[0])"
Update: s/the variable on which/the string on which/sub prematch (;$) { substr((@_ ? $_[0] : $_), 0, $-[0]) } sub match (;$) { substr((@_ ? $_[0] : $_), $-[0], $+[0] - $-[0]) } sub postmatch (;$) { substr((@_ ? $_[0] : $_), $+[0]) } my $foo = 'Hello, world!'; $foo =~ /o, w../; $\ = "\n"; print prematch($foo); print match($foo); print postmatch($foo); =head2 C<prematch>, C<match>, C<postmatch> Like C<$`>, C<$&> and C<$'>, but not slowing down, and needing a single argument: the string on which the most recent regex was performed. Uses C<$_> when no argument is given. =cut
- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(tye)Re: Why can't $` $& $' be enabled on a per-regex basis?
by tye (Sage) on Aug 10, 2002 at 16:39 UTC |