I know the following is heinous, & I also fussed with a number of solutions posed in earlier threads about similar questions. Unfortunately, none have worked thus far.
I'm wanting to define a regular expression which will be assigned to a constant, & obviously, I want to determine whether various strings match.
Assigning a pattern to a scalar value works:
#!/usr/bin/perl use strict; use Data::Dumper; my $regex = qr/^(\d{4})-(\d{2})-(\d{2})/; my $s = '2011-02-15'; my ($y, $m, $d) = ($s =~ /$regex/)[0, 1, 2]; print Data::Dumper->Dump([$y, $m, $d], [qw/y m d/]);
However, I'm getting spanked when redefining the pattern as a constant/subroutine (Syntax obviously wrong...):
#!/usr/bin/perl use strict; use Data::Dumper; use constant REGEX => qr/^(\d{4})-(\d{2})-(\d{2})/; my $s = '2011-02-15'; my ($y, $m, $d) = ($s =~ /&{[REGEX]}/)[0, 1, 2]; print Data::Dumper->Dump([$y, $m, $d], [qw/y m d/]);
Any help you can share would be greatly appreciated. I'm using Perl 5.8.8.
In reply to qr expression defined as constant? by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |