File Coverage

blib/lib/Socket/GetAddrInfo/Strict.pm
Criterion Covered Total %
statement 21 21 100.0
branch 3 4 75.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 33 34 97.0


line stmt bran cond sub pod time code
1             # You may distribute under the terms of either the GNU General Public License
2             # or the Artistic License (the same terms as Perl itself)
3             #
4             # (C) Paul Evans, 2011 -- leonerd@leonerd.org.uk
5              
6             package Socket::GetAddrInfo::Strict;
7              
8 2     2   32652 use strict;
  2         5  
  2         81  
9 2     2   11 use warnings;
  2         4  
  2         57  
10              
11 2     2   10 use Carp;
  2         5  
  2         209  
12              
13             our $VERSION = '0.22';
14              
15 2     2   12 use Exporter 'import';
  2         11  
  2         94  
16             our @EXPORT_OK = qw(
17             getaddrinfo
18             getnameinfo
19             );
20              
21 2     2   775 use Socket::GetAddrInfo ();
  2         4  
  2         442  
22              
23             # Re-export all the AI_*, EAI_* and NI_* constants
24             my @constants = @{ $Socket::GetAddrInfo::EXPORT_TAGS{constants} };
25             push @EXPORT_OK, @constants;
26             Socket::GetAddrInfo->import( @constants );
27              
28             =head1 NAME
29              
30             C - Provide L functions
31             which throw exceptions
32              
33             =head1 SYNOPSIS
34              
35             use Socket qw( SOCK_STREAM );
36             use Socket::GetAddrInfo::Strict qw( getaddrinfo getnameinfo );
37             use IO::Socket;
38              
39             my $sock;
40              
41             my %hints = ( socktype => SOCK_STREAM );
42             my @res = getaddrinfo( "www.google.com", "www", \%hints );
43              
44             while( my $ai = shift @res ) {
45              
46             $sock = IO::Socket->new();
47             $sock->socket( $ai->{family}, $ai->{socktype}, $ai->{protocol} ) or
48             undef $sock, next;
49              
50             $sock->connect( $ai->{addr} ) or undef $sock, next;
51              
52             last;
53             }
54              
55             if( $sock ) {
56             my ( $host, $service ) = getnameinfo( $sock->peername );
57             print "Connected to $host:$service\n";
58             }
59              
60             =head1 DESCRIPTION
61              
62             L provides the functions of C and
63             C, which return lists whose first element is error value, or
64             false indicating no error occured.
65              
66             This module wraps the functions provided by C to check
67             this error value, and throw an exception (using C) if an error occured.
68             If not, then the remaining values are returned as normal. This can simplify
69             the logic of a program which otherwise simply throws its own exception on
70             failure anyway.
71              
72             =cut
73              
74             =head1 FUNCTIONS
75              
76             =cut
77              
78             =head2 @res = getaddrinfo( $host, $service, $hints )
79              
80             After a successful lookup, returns the list of address structures, as
81             documented in L. If the lookup fails, an exception
82             containing the string form of the error is thrown instead.
83              
84             =cut
85              
86             sub getaddrinfo
87             {
88 2     2 1 5813 my ( $err, @res ) = Socket::GetAddrInfo::getaddrinfo( @_ );
89 2 100       24 die "$err\n" if $err;
90 1         5 return @res;
91             }
92              
93             =head2 ( $host, $service ) = getnameinfo( $addr, $flags, $xflags )
94              
95             After a successful lookup, returns the host and service name, as
96             documented in L. If the lookup fails, an exception
97             containing the string form of the error is thrown instead.
98              
99             =cut
100              
101             sub getnameinfo
102             {
103 1     1 1 1058 my ( $err, $host, $service ) = Socket::GetAddrInfo::getnameinfo( @_ );
104 1 50       6 die "$err\n" if $err;
105 1         3 return ( $host, $service );
106             }
107              
108             =head1 AUTHOR
109              
110             Paul Evans
111              
112             =cut
113              
114             0x55AA;