sourcecode
ybiC
<code>#!/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 script.
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
</code>
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. 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.
<p>
Thanks to [Petruchio] for the great code review [id://74600|below]. Thanks also to [jeffa], [chipmunk] and [buckaduck] for number-range-matching help. And to [tye] for further [id://74719|insight] on calling subroutines. Oh yeah, and to some guy named [vroom].
<p>
<i>Suggestions for improvement welcome and appreciated.</i>
<p>
<b>Updated: </b>
<br>2001-04-24
<br> Implemented most^W even more of [Petruchio|Pfunk]'s suggestions.
<br> <tt>perldoc ipdec2oct.pl</tt> for details.
Miscellaneous
ybiC