Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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.

In reply to Testing for valid package names by boftx

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (4)
    As of 2014-04-20 11:38 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (485 votes), past polls