in reply to RegEx Perl Code
$regex = qr/(?{print "Hey!";})/x; $string = "some word"; $string =~ /some($regex)/;
qr/STRING/imosx This operator quotes (and possibly compiles) its STRING as a regular expression. STRING is interpolated the same way as PATTERN in "m/PATTERN/". If "'" is used as the delimiter, no interpolation is done. Returns a Perl value which may be used instead of the corresponding "/STRING/imosx" expression. For example, $rex = qr/my.STRING/is; s/$rex/foo/; is equivalent to s/my.STRING/foo/is; The result may be used as a subpattern in a match: $re = qr/$pattern/; $string =~ /foo${re}bar/; # can be interpolated in +other patterns $string =~ $re; # or used standalone $string =~ /$re/; # or this way Since Perl may compile the pattern at the moment of execution of qr() operator, using qr() may have speed advantages in some situations, notably if the result of qr() is used standalone: sub match { my $patterns = shift; my @compiled = map qr/$_/i, @$patterns; grep { my $success = 0; foreach my $pat (@compiled) { $success = 1, last if /$pat/; } $success; } @_; } Precompilation of the pattern into an internal representation at the moment of qr() avoids a need to recompile the pattern every time a match "/$pat/" is attempted. (Perl has many other internal optimizations, but none would be triggered in the above example if we did not use qr() operator.)
# Under Construction
|
|---|