hagen has asked for the wisdom of the Perl Monks concerning the following question:
Is it possible to use a `named constant’ in a regex’s quantifiers?
I want to restrict the value of `word length’ to being stored in just the one place and so, using constant.pm (as described in Perl Debugged by Peter Scott and Ed Wright) I’ve come up with the following
#!/usr/local/perl/bin/perl use strict; use warnings; use constant WORD_LENGTH => 9; # define constant value in one plac +e my $Word_Length = WORD_LENGTH; # mmm, is this the best I can do? my $target_letters = 'x'; while ( $target_letters !~ /^[a-z]{$Word_Length}$/i ) { print "Enter the " . WORD_LENGTH . " letters for today\n"; $target_letters = <STDIN>; print "$target_letters\n"; } …
… which works fine, but requires the extra $Word_Length variable that, something tells me, a good Perlophone would eschew 8-)
I'm aware (Perl Debugged again) that the `name constant' is constructed as a subroutine when it's defined, and returns its value when it's called. So I've tried ...{WORD_LENGTH()} based on `if it looks like a function, it is a function'. I also tried ...{&WORD_LENGTH} both with and without `()'s.
Monks advice keenly sought… /hagen
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: subroutine within regex quantifier?
by chromatic (Archbishop) on Dec 04, 2001 at 09:39 UTC | |
by hagen (Friar) on Dec 04, 2001 at 10:26 UTC | |
Re: subroutine within regex quantifier?
by chipmunk (Parson) on Dec 04, 2001 at 09:54 UTC | |
Re: subroutine within regex quantifier?
by dws (Chancellor) on Dec 04, 2001 at 09:36 UTC | |
Re: subroutine within regex quantifier?
by hagen (Friar) on Dec 04, 2001 at 10:52 UTC |