Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
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!
  • 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, 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
  • 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [ambrus]: ah, it's one of thos
    [Eily]: what, there's a difference between proof of concept and production?
    [LanX]: 20 years ago traders were complaining about the latency of the trading system...
    [ambrus]: I'm currently in the process of rewriting my proof of concept programs. They sort of developped organically as I was experimenting, so now I've got an ugly mess of multiple programs and one-liners held together by nothing. I'll have to rewrite them to som
    [ambrus]: ething that's both cleanly organized and mostly automated.
    LanX in train, bad connection
    [Corion]: ambrus: Yeah - we're in that situation too, except that there is no time to do the reorganizing :-/
    [LanX]: ... so my boss started a project with the newest sun servers and invited the traders to come on weekend to test it... and they were so pleased, that they forced him to keep it in production...
    [ambrus]: Corion: sure, this is the long-term plan. The short term is that I have to run this ungodly mess to get results from the new input data today.
    [Corion]: ambrus: Most of our "automation" is tied to process exit codes and a shell pipeline :-\

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (16)
    As of 2017-03-29 11:49 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Should Pluto Get Its Planethood Back?



      Results (350 votes). Check out past polls.