AlexP has asked for the wisdom of the Perl Monks concerning the following question:
Hi monks, i need a script to convert ip's from string representation to bin to save them to my database later.
I came up with something like:
use v5.36; use Carp qw(croak); use Socket qw(AF_INET AF_INET6 inet_ntop inet_pton); use Data::Validate::IP qw(is_ipv4 is_ipv6); my $ip = $ARGV[0]; if (is_ipv4 $ip) { print inet_pton AF_INET, $ip; } elsif (is_ipv6 $ip) { print inet_pton AF_INET6, $ip; } else { croak 'Not a valid ip: ' . $ip; }
I'm interested in is there a shorter and more convenient way to do this in Perl or not?
For example, in php there is inet-pton that could take both ivp4 or ipv6 and do the right thing.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Pack ipv4 ipv6 to bin
by Tux (Canon) on Jul 17, 2022 at 11:07 UTC |