Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: qr expression defined as constant?
by jwkrahn (Abbot) on Feb 15, 2011 at 23:09 UTC | |
|
Re: qr expression defined as constant?
by ELISHEVA (Prior) on Feb 16, 2011 at 07:21 UTC | |
|
Re: qr expression defined as constant?
by wind (Priest) on Feb 15, 2011 at 23:40 UTC | |
|
Re: qr expression defined as constant?
by ikegami (Patriarch) on Feb 16, 2011 at 15:38 UTC |