G'day All,

I was absolutely certain that I'd seen a regex for checking module names somewhere in https://perldoc.perl.org/perl but, after checking through many documentation pages, I cannot locate it.

I came up with:

qr{^[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z0-9_]+)*$}

I'm aware that only has the "::" separator; not the (ancient) "'" separator.

I copied a number of module names from perlmodlib — which I hope covers all cases — and tested with:

#!/usr/bin/env perl use strict; use warnings; use Test::More; my @modules = qw{ strict autodie::exception::system utf8 B App::Prove::State::Result::Test CPAN::Meta::History::Meta_1_0 Encode::KR::2022_KR }; plan tests => 0+@modules; my $re = qr{^[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z0-9_]+)*$}; for my $mod (@modules) { is $mod =~ $re, !!1, "Testing $mod"; }

Output:

1..7 ok 1 - Testing strict ok 2 - Testing autodie::exception::system ok 3 - Testing utf8 ok 4 - Testing B ok 5 - Testing App::Prove::State::Result::Test ok 6 - Testing CPAN::Meta::History::Meta_1_0 ok 7 - Testing Encode::KR::2022_KR

If anyone knows about an official regex for this, please let me know.

If I am just imagining that I saw such a regex at some point in the past, I'll use what I have here. In that case, I'd appreciate knowing about any edge cases that I might have missed (or other improvements).

— Ken


In reply to Is there an official regex for checking module names? by kcott

Title:
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, details, 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, summary, 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.