Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
#!/usr/bin/perl -w # ipdec2oct.pl # pod at tail use strict; my @octets8; my $decdq = shift; unless (defined $decdq) { print("\n Hey you, enter a decimal dotted-quad!\n"); Usage(); } my @octets10 = (split /\./, $decdq); my $error = qq/\n Hey you, "$decdq" is baaad input:/; unless (@octets10 == 4) { print($error); print("\n It's not precisely 4 octets!\n"); Usage(); exit; } for (@octets10) { unless (/^\d{1,3}$/) { print($error); print(qq/\n "$_" is not a 1-3 digit positive integer!\n/); Usage(); } if ($_ > 255) { print($error); print(qq/\n "$_" is greater than 255!\n/); Usage(); } my $octet8 = sprintf "%lo", $_; push @octets8, ($octet8 > 7) ? "0$octet8" : $octet8; } print("\n decimal $decdq", "\n octal ", join('.', @octets8), "\n\n"); ###################################################################### +#### sub Usage { print("\n Usage: ipdec2oct.pl dottedquad_decimal_ipaddr\n"); print("\n $0\n Perl $]\n $^O\n\n"); exit; } ###################################################################### +#### =head1 Name ipdec2oct.pl =head1 Description convert decimal dotted-quad IP address to octal dq =head1 Usage ipdec2oct.pl dottedquad_decimal_ipaddr =head1 Tested Perl 5.00601 Win2kPro Cygwin Perl 5.00503 Win2kPro zshell(UnxUtils) and cmd.exe Perl 5.00503 Debian 2.2r3 =head1 Updated 2001-04-24 08:20 Moved 'unless (@octets10 == 4)' before 'for (@octets10)' loop for Great Justice. Er, for slight optimization. Minor format tweaks. 2001-04-23 16:15 Removed comments as err msg's sufficient. Mixed-case subroutine name, called with parens, w/o ampersan. Add $err to reduce redundant message text. 2001-04-22 Added '1-3 digit' to errmsg of 'positive interger'. Simplify octal-output padding with trinary op '? :'. Simplify octet parsing with $_ not declared scalar. Keep @octet10 otherwise need counter scalar. Replace %address with $decdq. Eliminate $address{octal} to simplify output code. Format for max of 80 character line. Check for (4) dot-separated octets in decimal input, Eliminate counter scalar by checking array length. Eliminate global vars by not using 'use vars qw()'. Use qq// to avoid leaning-toothpicks. Tweak is-octet-a-number regex for positive integers, so also eliminate if($_<0) loop. Minor format tweaks. 2001-04-21 Validate decimal dq input with numeric comparison, instead of only-partially-effective regexp. Confirm *something* entered with 'unless defined'. Pad octal output w/leading zero. Add comments and pod. Post to Perl Monks. 2001-03-20 Initial working code. =head1 Todos Sit back and chuckle over time+synapses applied to a mostly useless s +cript. Set to memory goodies learned of split join oct qq ?: array-length defined regexen padding global-vs-lexical calling&naming-subs optimization. =head1 Author ybiC =head1 Credits Thanks to Petruchio for greatly appreciated code review, and for interesting observation that inspired this exercise. And to jeffa, chipmunk and buckaduck for number-range-matching help. And to tye for further insight on calling subroutines. Oh yeah, and to some guy named vroom. =cut

In reply to (code) IP address - decimal to octal by ybiC

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 wandering the Monastery: (3)
    As of 2014-11-29 08:47 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (204 votes), past polls