http://www.perlmonks.org?node_id=512913


in reply to Re: Elegant way to split into sequences of identical chars?
in thread Elegant way to split into sequences of identical chars?

That's really cool, but I'm confused as to why it works. From perlre about (??{ code }):

This is a "postponed" regular subexpression. The "code" is evaluated at run time, at the moment this subexpression may match. The result of evaluation is considered as a regular expression and matched as if it were inserted instead of this construct.

Maybe I'm thrown by the "matched as if it were inserted" part when what's being inserted is a regular expression -- if it were just inserted, I don't understand why your approach works different from just putting the subexpression in directly. It looks like it's being evaluated somehow "separately" from the rest of the regular expression, which I didn't expect given the doc. I don't know if that makes sense, but this code sample illustrates where I'm thrown:

my $re = qr/(.)\1*/; @matches = $string =~ m/($re)/g; print "@matches\n"; # x x x x 5 5 5 5 6 6 x x x x @matches = $string =~ m/((??{$re}))/g; print "@matches\n"; # xx 55 6 xx

Could you please help me understand what's really going on that's different between these two cases?

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.