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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |