<?xml version="1.0" encoding="windows-1252"?>
<node id="74513" title="(code) IP address - decimal to octal" created="2001-04-22 02:49:11" updated="2005-08-11 06:29:59">
<type id="1748">
sourcecode</type>
<author id="14909">
ybiC</author>
<data>
<field name="doctext">
&lt;code&gt;#!/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 ($_ &gt; 255) {
        print($error);
        print(qq/\n  "$_" is greater than 255!\n/);
        Usage();
        }
    my $octet8 = sprintf "%lo", $_;
    push @octets8, ($octet8 &gt; 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($_&lt;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 script.
 Set to memory goodies learned of
   split    join    oct
   qq    ?:    array-length
   defined    regexen    padding
   global-vs-lexical   calling&amp;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

&lt;/code&gt;</field>
<field name="codedescription">
Convert a dotted-quad decimal IP address to it's dq octal form, and give a slightly rude retort if you don't know what constitutes a valid decimal dq. &amp;nbsp; Inspired by [Petruchio|bruddaP]'s observation that pinging an IP address that's been leading-zero-padded results in ICMP echo request sent to unexpected host.

&lt;p&gt;
Thanks to [Petruchio] for the great code review [id://74600|below]. &amp;nbsp; Thanks also to [jeffa], [chipmunk] and [buckaduck] for number-range-matching help. &amp;nbsp; And to [tye] for further [id://74719|insight] on calling subroutines. &amp;nbsp; Oh yeah, and to some guy named [vroom].

&lt;p&gt;
&lt;i&gt;Suggestions for improvement welcome and appreciated.&lt;/i&gt;

&lt;p&gt;
&lt;b&gt;Updated: &lt;/b&gt;
&lt;br&gt;2001-04-24
&lt;br&gt; &amp;nbsp; &amp;nbsp; Implemented most^W even more of [Petruchio|Pfunk]'s suggestions.
&lt;br&gt; &amp;nbsp; &amp;nbsp; &lt;tt&gt;perldoc ipdec2oct.pl&lt;/tt&gt; for details.</field>
<field name="codecategory">
Miscellaneous</field>
<field name="codeauthor">
ybiC</field>
</data>
</node>
