in reply to how to use string RE (against $_ and capture)

G'day perl-diddler,

Firstly, I suggest you read "perlop: Regexp Quote-Like Operators". There are certain things about qr that are non-standard and non-intuitive: I suspect you're making understandable, yet incorrect, assumptions.

In the following, I've addressed all (I think) of the points you've raised and hopefully cleared up any misunderstandings.

When used as a string, qr acts like a string:

$ perl -E 'my $re = qr{([0-6BS])}; say $re' (?^u:([0-6BS]))

When used as a reference, qr acts like a reference:

$ perl -E 'my $re = qr{([0-6BS])}; say ref($re)' Regexp

When used as a hash key, which is a string, qr acts like a string:

$ perl -E 'my $re = qr{([0-6BS])}; my %x = ($re, 1); my @y = keys %x; +say $y[0]' (?^u:([0-6BS]))

The hash key is just a string, not a reference:

$ perl -E 'my $re = qr{([0-6BS])}; my %x = ($re, 1); my @y = keys %x; +say "|", ref($y[0]), "|"' ||

The next two points directly address the title of your post: "how to use string RE (against $_ and capture)".

You can use qr in a match like '/COMPILED_QR_RE/':

$ perl -E 'my $re = qr{([0-6BS])}; $_ = "B"; /$re/; say $1' B

You can use a hash key string in a match like '/STRING_RE/':

$ perl -E 'my $re = qr{([0-6BS])}; my %x = ($re, 1); my @y = keys %x; +$_ = "B"; /$y[0]/; say $1' B

See also: perlre and ref.

— Ken