perlquestion
boftx
<p>
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 <c>\w</c>.
</p>
<p>
My questions are these:
<ol>
<li><strike>Is my character set too restrictive, or is it reasonable in light of the majority of use-cases and portability concerns?</strike></li>
<li><strike>Should I remove the "_" character from the list?</strike></li>
<li>What improvements can be made to the test cases below to better test edge cases?</li>
</ol>
</p>
<p>
Here is the test code:
</p>
<c>
#!/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__
</c>
<p>
Update: Added <c>foo.pm</c> to list of bad names.
</p>
<p>
Update 2: Questions 1 and 2 are answered directly by the code from [mod://Module::Runtime] supplied below by [tobyink]
</p>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-1049510">
It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
</div></div>