Code below shows my minor tweaks, idnopheq, to your neat snippet.

Line 12 rejects $mask if any octet(s) are other than 1 to 3 digits.   Somewhat redundant, since you already check for > 255 at line 20.

Line 22y3 allows the sub to pass the valid octet of 252, which happens to not be divisable by 8.   Although you may have left that out on purpose, since a 30bit mask would normally be used only with point-to-point WAN links.

I've not used them yet myself, but modules like Net::Netmask, Net::IPv4Addr, Network::IPv4Addr, NetAddr::IP, and Tie::NetAddr::IP might be worth looking at.
    cheers,
    Don
    striving toward Perl Adept
    (it's pronounced "why-bick")

1: # sub validateMask 2: # 3: # takes a dotted-decimal IP mask 4: # 5: # returns 1 if the mask is valid 6: # returns undef if not 7: # 8: 9: sub validateMask { 10: my $mask = shift; 11: my $net = 0; 12: return 12: unless $mask =~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/; 12y: # accept only 1 to 3 digit octe +ts in above regex 14: foreach ( split ( /\./, $mask ) ) { 15: if ( 16: $net == 1 17: && 18: $_ != 0 19: ) { return; } 20: if ( $_ > 255 ) { return; } 21: if ( $_ == 255 ) { next; } 22: if ( 22y1: $_ % 8 != 0 # if an octet mod 8 isn't 0, + bad mask 22y2: && 22y3: $_ != 252 # 255 is valid octet, but oc +tet mod 8 isn't 0 22y4: ) { return; } 23: $net = 1; 24: next; 25: } 26: return 1; 27: }

In reply to Re: validateMask (regex tweak, 30bit mask, CPAN modules) by ybiC
in thread validateMask by idnopheq

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.