Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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 (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

In reply to subroutine within regex quantifier? by hagen

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (5)
    As of 2021-05-06 13:29 GMT
    Find Nodes?
      Voting Booth?
      Perl 7 will be out ...

      Results (75 votes). Check out past polls.