Hi,
I'd like some help from anyone who can explain how the following pack/unpack examples work (in simple terms please :-) ).
All the examples I've looked at on the web mention the B32 format only briefly, use binary representation instead of decimal (as used by this prog) and only have 1 value after the 'B32, instead of 2 values as below.
The code below is based on a program I need to amend.
I'd also like to know why the 2 different ways of retrieving the mask both work/produce the same answer.
Thanks for any help
Cheers
Chris
#!/usr/bin/perl -w
+
use IO::Socket;
use strict;
+
my (
$key, %new,
$str1, $str2, $str3,
$mask, $ip, $ip_disp, $mask_disp
);
+
$str1 = '202.6.145.136';
$str2 = '29';
$str3 = '29';
print "$str1, $str2, $str3\n";
$mask = pack ('B32', (1 x $str2), (1 x (32 - $str2)));
$new {AA} {inet_aton ($str1) & $mask} =
$mask . pack ('B32', (1 x $str3), (1 x (32 - $str3
+)));
+
for $ip ( keys %{$new{AA}} )
{
$ip_disp = join('.',unpack('CCCC',$ip));
print "unpack ip CCCC = $ip_disp\n\n";
+
$mask_disp = join('.',unpack('CCCC',$new{AA}{$ip}));
print "unpack mask CCCC = $mask_disp\n\n";
+
$mask_disp = join('.',unpack('CCCC',substr($new{AA}->{$ip},0,4)));
print "substr/unpack CCCC mask = $mask_disp\n";
}
exit;
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.