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:
Is my character set too restrictive, or is it reasonable in light of the majority of use-cases and portability concerns?Should I remove the "_" character from the list?- 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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Testing for valid package names
by tobyink (Canon) on Nov 25, 2013 at 21:56 UTC | |
by boftx (Deacon) on Nov 25, 2013 at 22:08 UTC | |
Re: Testing for valid package names
by toolic (Bishop) on Nov 25, 2013 at 21:15 UTC | |
by boftx (Deacon) on Nov 25, 2013 at 21:20 UTC | |
by LanX (Saint) on Nov 25, 2013 at 21:24 UTC | |
by boftx (Deacon) on Nov 25, 2013 at 21:36 UTC | |
Re: Testing for valid package names
by taint (Chaplain) on Nov 25, 2013 at 21:20 UTC | |
by boftx (Deacon) on Nov 25, 2013 at 21:27 UTC | |
by taint (Chaplain) on Nov 25, 2013 at 21:33 UTC | |
by taint (Chaplain) on Nov 25, 2013 at 21:46 UTC |
Back to
Seekers of Perl Wisdom