Yes, use Net::IP instead.

#!/pro/bin/perl use 5.018003; use warnings; use Net::IP; use Socket qw(AF_INET AF_INET6 inet_ntop inet_pton); use Data::Validate::IP qw(is_ipv4 is_ipv6); my $ip = $ARGV[0] or die "usage: $0 IP\n"; say "Using Net::IP"; if (my $nip = Net::IP->new ($ip)) { say "IP*"; my $bip = pack "B*" => $nip->binip (); if (length ($bip) == 4) { say join "." => unpack "C*" => inet_pton (AF_INET, $ip); } else { say join ":" => map { sprintf "%04x", $_ } unpack "S>*" => inet_pton (AF_INET6, $ip); } } say ""; say "Using Data::Validate::IP"; if (is_ipv4 ($ip)) { say "IPv4!"; say join "." => unpack "C*" => inet_pton (AF_INET, $ip); } elsif (is_ipv6 ($ip)) { say "IPv6!"; say join ":" => map { sprintf "%04x", $_ } unpack "S>*" => inet_pton (AF_INET6, $ip); } else { die "Not a valid ip: $ip\n"; }

->

$ test.pl 192.168.14.5 Using Net::IP IP* 192.168.14.5 Using Data::Validate::IP IPv4! 192.168.14.5 $ test.pl 0203:3564:a5a5:0001:1234:4321:abcd:fdba Using Net::IP IP* 0203:3564:a5a5:0001:1234:4321:abcd:fdba Using Data::Validate::IP IPv6! 0203:3564:a5a5:0001:1234:4321:abcd:fdba

Enjoy, Have FUN! H.Merijn

In reply to Re: Pack ipv4 ipv6 to bin by Tux
in thread Pack ipv4 ipv6 to bin by AlexP

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, 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:
    & &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.