#!/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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.