http://www.perlmonks.org?node_id=1064292

boftx has asked for the wisdom of the Perl Monks concerning the following question:

I have a need to test a string to see if it would be a valid name for a package suitable for submission to CPAN. Currently, I am taking a rather draconic approach and allowing only the characters that are matched by \w.

My questions are these:

  1. Is my character set too restrictive, or is it reasonable in light of the majority of use-cases and portability concerns?
  2. Should I remove the "_" character from the list?
  3. What improvements can be made to the test cases below to better test edge cases?

Here is the test code:

#!/usr/bin/perl -T use 5.008_008; use strict; use warnings FATAL => 'all'; use Test::More; my @good_pnames = ( qw( foo foo::bar foo_bar foo::bar_baz ) ); my @bad_pnames = ( qw( foo.pm foo! foo: foo:: foo::! foo:bar foo::bar! foo::bar:baz ) ); push( @bad_pnames, 'foo bar' ); for (@good_pnames) { ok( valid_pname($_), "$_ is valid" ); } for (@bad_pnames) { ok( !valid_pname($_), "$_ is not valid" ) or BAIL_OUT("Invalid package was accepted! $_"); } done_testing(); exit; sub valid_pname { my $pname = shift; return !!($pname =~ /^\w+(?:::\w+)*$/); } __END__

Update: Added foo.pm to list of bad names.

Update 2: Questions 1 and 2 are answered directly by the code from Module::Runtime supplied below by tobyink

It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.