Quiq/Socket.pm

filter: Statementstmt 77.1% 27 / 35 Branchbran 25.0% 1 / 4 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 77.7% 7 / 9 Podpod 100% 3 / 3 totaltotal 69.6% 39 / 56 CC 4 SCAR 9.0
CC
4
Cov
69%
close
6.0
new
3.2
slurp
2.0
CRAP 2.5
« Quiq/Gd/Component.pm ↑ Summary Quiq/ContentProcessor/BaseType.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Socket - TCP-Verbindung zu einem Server
8  
9 =head1 BASE CLASSES
10  
11 =over 2
12  
13 =item *
14  
15 IO::Socket::INET
16  
17 =item *
18  
19 L<Quiq::Object>
20  
21 =back
22  
23 =head1 DESCRIPTION
24  
25 Ein Objekt der Klasse repräsentiert eine TCP-Verbindung zu einem Server.
26 Der Verbindungsaufbau erfolgt mit der Instantiierung des Objekts.
27  
28 Die Klasse ist abgeleitet von IO::Socket::INET und besitzt folglich
29 alle Methoden dieser Klasse.
30  
31 =head1 EXAMPLE
32  
33 Sende GET-Request an Google und gib die Antwort aus:
34  
35 my $sock = Quiq::Socket->new('google.com',80);
36 print $sock "GET /\n";
37 while (<$sock>) {
38 print;
39 }
40  
41 =cut
42  
43 # -----------------------------------------------------------------------------
44  
45 package Quiq::Socket;
46 2 ▶use base qw/IO::Socket::INET Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 2 our $VERSION = '1.240';
54  
55 2 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 # -----------------------------------------------------------------------------
58  
59 =head1 METHODS
60  
61 =head2 Konstruktor
62  
63 =head3 new() - Instantiiere Socket
64  
65 =head4 Synopsis
66  
67 $sock = $class->new($host,$port,@opt);
68  
69 =head4 Options
70  
71 =over 4
72  
73 =item -sloppy => $bool (Default: 0)
74  
75 Wirf keine Exception, wenn der Verbindungsaufbau fehlschlägt, sondern
76 liefere C<undef>.
77  
78 =back
79  
80 =head4 Description
81  
82 Baue eine TCP-Verbindung zu Host $host und Port $port auf
83 und liefere eine Referenz auf das Socket-Objekt zurück.
84  
85 =cut
86  
87 # -----------------------------------------------------------------------------
88  
89 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.7
Pod documented
90 1 my $class = shift;
91 1 my $host = shift;
92 1 my $port = shift;
93 # @_: @opt
94  
95 # Optionen
96  
97 1 my $sloppy = 0;
98 1 Quiq::Option->extract(\@_,
99 -sloppy => \$sloppy,
100 );
101  
102 # Baue Verbindung auf
103  
104 1 my $self = $class->SUPER::new("$host:$port");
105 1 ▶ if (!$self && !$sloppy) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self or $sloppy) 0 1 1
Condition33%1/3
$self or $sloppy
l!l&&r!l&&!r
100
MC/DC0%0/2
$self or $sloppy
$self $sloppy
Truth table33%1/3
A or B
A= $selfB= $sloppy
ABresult
1X1
011
000
106 0 $class->throw(
107 'SOCK-00001: Verbindungsaufbau fehlgeschlagen',
108 Host => $host,
109 Port => $port,
110 );
111 }
112  
113 1 return $self;
114 }
115  
116 # -----------------------------------------------------------------------------
117  
118 =head2 Objektmethoden
119  
120 =head3 slurp() - Lies alle Daten
121  
122 =head4 Synopsis
123  
124 $data = $sock->slurp;
125  
126 =head4 Description
127  
128 Lies alle Daten bis der Socket geschlossen wird. Die Methode ist
129 nützlich, um eine HTTP-Antwort zu lesen.
130  
131 =cut
132  
133 # -----------------------------------------------------------------------------
134  
135 0 ▶sub slurp {
Subroutine slurp: not called CC=1 SCAR=6.9
Pod documented
136 0 my $self = shift;
137 0 local $/;
138 0 return <$self>;
139 }
140  
141 # -----------------------------------------------------------------------------
142  
143 =head3 close() - Schließe Socket
144  
145 =head4 Synopsis
146  
147 $sock->close;
148  
149 =cut
150  
151 # -----------------------------------------------------------------------------
152  
153 0 ▶sub close {
Subroutine close: not called CC=2 SCAR=17.9
Pod documented
154 0 my $self = shift;
155  
156 0 ▶ CORE::close $self or do {
Branch: 1 branch
BranchTrueFalseTotal
CORE::close $self or do { $self->throw("SOCK-00002: Socket schlie\x{df}en fehlgeschlagen") } 0 0 0
157 0 $self->throw('SOCK-00002: Socket schließen fehlgeschlagen');
158 };
159  
160 0 return;
161 }
162  
163 # -----------------------------------------------------------------------------
164  
165 =head1 VERSION
166  
167 1.240
168  
169 =head1 AUTHOR
170  
171 Frank Seitz, L<http://fseitz.de/>
172  
173 =head1 COPYRIGHT
174  
175 Copyright (C) 2026 Frank Seitz
176  
177 =head1 LICENSE
178  
179 This code is free software; you can redistribute it and/or modify
180 it under the same terms as Perl itself.
181  
182 =cut
183  
184 # -----------------------------------------------------------------------------
185  
186 2 1;
187  
188 # eof
« Quiq/Gd/Component.pm ↑ Summary Quiq/ContentProcessor/BaseType.pm »