File: Strict.pm

package info (click to toggle)
libsocket-getaddrinfo-perl 0.22-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 452 kB
  • sloc: perl: 964; makefile: 14
file content (114 lines) | stat: -rw-r--r-- 2,727 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#  You may distribute under the terms of either the GNU General Public License
#  or the Artistic License (the same terms as Perl itself)
#
#  (C) Paul Evans, 2011 -- leonerd@leonerd.org.uk

package Socket::GetAddrInfo::Strict;

use strict;
use warnings;

use Carp;

our $VERSION = '0.22';

use Exporter 'import';
our @EXPORT_OK = qw(
   getaddrinfo
   getnameinfo
);

use Socket::GetAddrInfo ();

# Re-export all the AI_*, EAI_* and NI_* constants
my @constants = @{ $Socket::GetAddrInfo::EXPORT_TAGS{constants} };
push @EXPORT_OK, @constants;
Socket::GetAddrInfo->import( @constants );

=head1 NAME

C<Socket::GetAddrInfo::Strict> - Provide L<Socket::GetAddrInfo> functions
which throw exceptions

=head1 SYNOPSIS

 use Socket qw( SOCK_STREAM );
 use Socket::GetAddrInfo::Strict qw( getaddrinfo getnameinfo );
 use IO::Socket;

 my $sock;

 my %hints = ( socktype => SOCK_STREAM );
 my @res = getaddrinfo( "www.google.com", "www", \%hints );

 while( my $ai = shift @res ) {

    $sock = IO::Socket->new();
    $sock->socket( $ai->{family}, $ai->{socktype}, $ai->{protocol} ) or
       undef $sock, next;

    $sock->connect( $ai->{addr} ) or undef $sock, next;

    last;
 }

 if( $sock ) {
    my ( $host, $service ) = getnameinfo( $sock->peername );
    print "Connected to $host:$service\n";
 }

=head1 DESCRIPTION

L<Socket::GetAddrInfo> provides the functions of C<getaddrinfo> and
C<getnameinfo>, which return lists whose first element is error value, or
false indicating no error occurred.

This module wraps the functions provided by C<Socket::GetAddrInfo> to check
this error value, and throw an exception (using C<die>) if an error occurred.
If not, then the remaining values are returned as normal. This can simplify
the logic of a program which otherwise simply throws its own exception on
failure anyway.

=cut

=head1 FUNCTIONS

=cut

=head2 @res = getaddrinfo( $host, $service, $hints )

After a successful lookup, returns the list of address structures, as
documented in L<Socket::GetAddrInfo>. If the lookup fails, an exception
containing the string form of the error is thrown instead.

=cut

sub getaddrinfo
{
   my ( $err, @res ) = Socket::GetAddrInfo::getaddrinfo( @_ );
   die "$err\n" if $err;
   return @res;
}

=head2 ( $host, $service ) = getnameinfo( $addr, $flags, $xflags )

After a successful lookup, returns the host and service name, as
documented in L<Socket::GetAddrInfo>. If the lookup fails, an exception
containing the string form of the error is thrown instead.

=cut

sub getnameinfo
{
   my ( $err, $host, $service ) = Socket::GetAddrInfo::getnameinfo( @_ );
   die "$err\n" if $err;
   return ( $host, $service );
}

=head1 AUTHOR

Paul Evans <leonerd@leonerd.org.uk>

=cut

0x55AA;