Re: regex, number of matches

Dr.Ruud (rvtol+news@xxxxxxxxxxxx) wrote on MMMMCDVIII September MCMXCIII
in <URL:news:dh5t7s.1g0.1@xxxxxxxxxxxxxxxxx>:
{} Tad McClellan:
{} > Dr.Ruud:
{} >> I find it hard to think of a reason why the first use of $& should
{} >> harm all other pattern matches.
{} >
{} > Because a whole bunch of characters must be stored for
{} > every (successful) pattern match.
{} In the circumstances we were discussing, an offset plus length on the
{} input buffer is all you need there (and those are known values).

But it's not easy to determine those special circumstances. In general
an offset won't do as the string might change. Consider:

my $line = "Hello, world";
if ($line =~ /\w+$/) {
substr ($line, 7) = "earthlings";
print $&, "\n";

{} I think that the alternatives "s/(\S+)/$1/g" and "s/\S+/$&/g" don't need
{} behave differently.

True, if there are no further references to $&. But that's a special case,
and perl doesn't spend CPU cycles to find out. You're welcome to provide
a patch, although with all the warnings against using $& already in the
documentation, I'm not sure the patch will be accepted.

INIT {print "Perl " }
END {print "Hacker\n"}
CHECK {print "another "}
BEGIN {print "Just " }