$& discussion

Michael Dearman mdearman at
Thu Aug 24 05:07:17 CDT 2000

Hi guys,
Enjoyed the get together and meeting you'all.

Doug Miles wrote:
> I looked into the $& problem in O'reilly's "Mastering Regular
> Expressions",

For Perl ver. 5.003, don't have a clue if this still holds. The author
has maintained an excellent support site, but I don't remember seeing
anything addressing this issue. 

>               and found that the inefficiency is caused by perl making a
> copy of the string that is being matched against.  This is also caused
> by using '()'s in a regex (which I am doing), so avoiding the use of $&

Just to round the culprits out - this also happens when $' and $` are used.
As I read it, this group and parens also disable some optimizations done for
substitutions. But for parens,this only affects the regex actually in the parens.

and the /i modifier causes the copy, in some cases.

Also, you'll get this performance hit if these are in any module/lib your using.
There's a list of the offending libs on pg. 278, Mastering Reg Expressions.
Of course some popular modules are on the list.

> won't help.  Also, with the addition of \s* to my pattern (which I have

I'm probably missing something, but why can't you just do 'length($1)',
for $match_length?

Mike D.

> done), I don't think I can avoid using $&.  Great discussion!
> --
> - Doug
> Don't anthropomorphize computers. They hate that.

More information about the Phoenix-pm mailing list