regexp_pattern#

Returns the pattern of a compiled regular expression, and in list context its modifiers as well.

Synopsis#

use re qw(regexp_pattern);
my $qr = qr/(?<y>\d{4})-(?<m>\d\d)/i;
my ($pat, $mods) = regexp_pattern($qr);
# $pat  is '(?<y>\d{4})-(?<m>\d\d)', $mods is 'i'
my $str = regexp_pattern($qr);
# $str  is '(?^i:(?<y>\d{4})-(?<m>\d\d))'

In list context it returns two elements: the pattern and the modifiers it was compiled with. In scalar context it returns what perl gives when it stringifies the qr//.

For an argument that is not a compiled regexp it returns the empty list in list context and a false but defined value in scalar context, so regexp_pattern($ref) eq '(?^i:foo)' never warns, whatever $ref holds. Like is_regexp, it is not fooled by overloading or blessing.