Quiq/FileHandle.pm

filter: Statementstmt 75.1% 136 / 181 Branchbran 50.0% 38 / 76 Conditioncond 61.2% 19 / 31 MC/DCmcdc 17.6% 3 / 17 Subroutinesub 86.1% 31 / 36 Podpod 100% 22 / 22 totaltotal 68.6% 249 / 363 CC 50 SCAR 25.0
CC
50
Cov
67%
new
34.2
readLines
30.0
captureStderr
12.0
CRAP 12.2
« Quiq/Html/Pygments.pm ↑ Summary Quiq/StreamServe/Block.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::FileHandle - Datei-Handle
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =head1 SYNOPSIS
14  
15 Datei schreiben:
16  
17 my $fh = Quiq::FileHandle->new('>',$path);
18 $fh->print("Test\n");
19 $fh->close;
20  
21 Datei lesen:
22  
23 my $fh = Quiq::FileHandle->new('<',$path);
24 while (<$fh>) {
25 print;
26 }
27 $fh->close;
28  
29 Programm darf nur einmal laufen:
30  
31 my $fh = eval{Quiq::FileHandle->new('<',$file,-lock=>'EXNB')};
32 if ($@) {
33 exit 5;
34 }
35  
36 Zähler-Datei mit Locking:
37  
38 my $fh = Quiq::FileHandle->new('+>>',$file,-lock=>'EX');
39 $fh->seek(0);
40 my $count = <$fh> || "0\n";
41 chomp $count;
42 $fh->truncate;
43 $fh->print(++$count,"\n");
44
45 # Der Lock bleibt so lange bestehen bis $fh aus dem Scope geht
46  
47 Siehe auch Quiq::LockedCounter.
48  
49 =head1 DESCRIPTION
50  
51 Ein Objekt der Klasse repräsentiert eine Dateihandle, über die
52 Daten gelesen oder geschrieben werden können.
53  
54 =cut
55  
56 # -----------------------------------------------------------------------------
57  
58 package Quiq::FileHandle;
59 270 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 270 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 270 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 270 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 270 our $VERSION = '1.240';
66  
67 270 ▶use Quiq::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 270 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 270 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
70 270 ▶use Quiq::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 270 ▶no bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 270 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 # -----------------------------------------------------------------------------
75  
76 =head1 METHODS
77  
78 =head2 Konstruktor
79  
80 =head3 new() - Öffne Dateihandle
81  
82 =head4 Synopsis
83  
84 $fh = $class->new($mode,$path,@opt);
85 $fh = $class->new($globRef,@opt);
86 $fh = $class->new('<'); # Lesen von STDIN
87 $fh = $class->new('>'); # Schreiben nach STDOUT
88  
89 =head4 Alias
90  
91 open()
92  
93 =head4 Options
94  
95 =over 4
96  
97 =item -createDir => $bool (Default: 0)
98  
99 Erzeuge den Verzeichnispfad einer Datei, die geschrieben wird,
100 falls er nicht existiert.
101  
102 =item -lock => 'EX'|'SH'|'EXNB'|'SHNB' (Default: kein Lock)
103  
104 Locke die Dateihandle nach dem Öffnen im angegebenen Lock-Modus.
105 Folgende Lockmodes werden unterschieden: 'SH' (shared lock), 'EX'
106 (exclusive lock). Durch den Zusatz 'NB' (also Lockmode 'SHNB' der 'EXNB')
107 wird die Operation "non blocking" ausgeführt, d.h. wenn der Lock nicht
108 sofort erworben werden kann, wird eine Exception ausgelöst.
109  
110 Wurde die Datei vom Konstruktor geöffnet, schließt er sie, wenn
111 der Lock nicht erworben werden kann. Andernfalls bleibt die
112 Dateihandle geöffnet.
113  
114 =back
115  
116 =head4 Description
117  
118 Instantiiere Dateihandle-Objekt und liefere eine Referenz auf dieses
119 Objekt zurück.
120  
121 =head4 Examples
122  
123 Filehandle-Objekt für STDOUT:
124  
125 $fh = Quiq::FileHandle->new(\*STDOUT);
126  
127 Lesen von STDIN:
128  
129 $fh = $class->new('<');
130 $fh = $class->new('<','');
131 $fh = $class->new('<','-');
132  
133 Schreiben nach STDOUT:
134  
135 $fh = $class->new('>');
136 $fh = $class->new('>','');
137 $fh = $class->new('>','-');
138  
139 =cut
140  
141 # -----------------------------------------------------------------------------
142  
143 114 ▶sub new {
Subroutine new: called CC=20 SCAR=35.3
Pod documented
144 114 my $class = shift;
145 # @_: ($globRef,@opt) -oder- ($mode,$path,@opt)
146  
147 114 my ($self,$mode,$path);
148 114 ▶ if (ref $_[0] eq 'GLOB') { # GLOB-Referenz
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'GLOB') { } 0 114 114
149 0 $self = shift;
150 }
151 else { # Datei öffnen
152 114 $mode = shift;
153 114 $path = shift;
154 114 ▶ if (!ref $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $path) 92 22 114
155 92 $path = Quiq::Path->expandTilde($path);
156 }
157 }
158  
159 # Optionen
160  
161 114 my $lock;
162 my $createDir;
163  
164 114 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 112 114
165 2 Quiq::Option->extract(\@_,
166 -createDir=>\$createDir,
167 -lock=>\$lock,
168 );
169 }
170  
171 114 ▶ if ($mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode) 114 0 114
172 114 ▶ if ($mode eq '<' && (!$path || $path eq '-')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq '<' and !$path || $path eq '-') { } 0 114 114
elsif ($mode eq '>' and !$path || $path eq '-') { } 0 114 114
Condition50%3/6
$mode eq "<" and !$path || $path eq "-"
!ll&&!rl&&r
23910
not $path or $path eq "-"
l!l&&r!l&&!r
0091
MC/DC0%0/3
$mode eq "<" and !$path || $path eq "-"
$mode eq "<" not $path $path eq "-"
Truth table50%2/4
A and not(B) or C
A= $mode eq "<"B= not $pathC= $path eq "-"
ABCresult
0XX0
1000
11X1
1011
173 0 $self = \*STDIN;
174 }
175 ▶ elsif ($mode eq '>' && (!$path || $path eq '-')) {
Condition50%3/6
$mode eq ">" and !$path || $path eq "-"
!ll&&!rl&&r
93210
not $path or $path eq "-"
l!l&&r!l&&!r
0021
MC/DC0%0/3
$mode eq ">" and !$path || $path eq "-"
$mode eq ">" not $path $path eq "-"
Truth table50%2/4
A and not(B) or C
A= $mode eq ">"B= not $pathC= $path eq "-"
ABCresult
0XX0
1000
11X1
1011
176 0 $self = \*STDOUT;
177 }
178 else {
179 114 ▶ if (ref($path) && Scalar::Util::reftype($path) eq 'SCALAR' &&
Branch: 1 branch
BranchTrueFalseTotal
if (ref $path and builtin::reftype($path) eq "SCALAR" and ref $path ne "SCALAR") 1 113 114
Condition100%6/6
ref $path and builtin::reftype($path) eq "SCALAR" and ref $path ne "SCALAR"
!ll&&!rl&&r
97161
ref $path and builtin::reftype($path) eq "SCALAR"
!ll&&!rl&&r
92517
MC/DC100%3/3
ref $path and builtin::reftype($path) eq "SCALAR" and ref $path ne "SCALAR"
ref $path builtin::reftype($path) eq "SCALAR" ref $path ne "SCALAR"
Truth table100%4/4
A and B and C
A= ref $pathB= builtin::reftype($path) eq "SCALAR"C= ref $path ne "SCALAR"
ABCresult
0XX0
10X0
1100
1111
180 ref($path) ne 'SCALAR') {
181 # Wenn $path eine *geblesste* Skalarreferenz ist,
182 # müssen wir die Daten kopieren, um eine ungeblesste
183 # Referenz zu bekommen, denn open() arbeitet nicht auf
184 # auf einer geblessten Referenz
185 1 $path = \(my $tmp = $$path);
186 }
187  
188 114 ▶ if ($mode eq '>' && $createDir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq ">" and $createDir) 0 114 114
Condition66%2/3
$mode eq ">" and $createDir
!ll&&!rl&&r
93210
MC/DC0%0/2
$mode eq ">" and $createDir
$mode eq ">" $createDir
Truth table66%2/3
A and B
A= $mode eq ">"B= $createDir
ABresult
0X0
100
111
189 0 Quiq::Path->mkdir($path,-createParent=>1);
190 }
191  
192 114 ▶ unless (open $self,$mode,$path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (CORE::open $self, $mode, $path) 0 114 114
193 0 $class->throw(
194 'FH-00001: Can\'t open file',
195 Path=>$path,
196 Mode=>$mode,
197 Errstr=>$!,
198 );
199 }
200 }
201 }
202 114 $self = bless $self,$class;
203  
204 # Datei locken
205  
206 114 ▶ if ($lock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lock) 2 112 114
207 2 ▶ $self->lock($lock) or do {
Branch: 1 branch
BranchTrueFalseTotal
$self->lock($lock) or do { if ($path) { CORE::close $self; } ; $class->throw("FH-00002: Can't acquire lock", "Errstr", $!) } 2 0 2
208 0 ▶ if ($path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 0 0 0
209 # Wir haben die Datei geöffnet und schließen sie gleich wieder
210 0 CORE::close $self;
211 }
212 0 $class->throw('FH-00002: Can\'t acquire lock',Errstr=>$!);
213 };
214 }
215  
216 114 return $self;
217 }
218  
219 {
220 270 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
221 270 *open = \&new;
222 }
223  
224 # -----------------------------------------------------------------------------
225  
226 =head3 close() - Schließe Dateihandle
227  
228 =head4 Synopsis
229  
230 $fh->close;
231  
232 =head4 Alias
233  
234 destroy()
235  
236 =head4 Description
237  
238 Schließe Dateihandle. Die Methode liefert keinen Wert zurück.
239 Nach Aufruf der Methode ist die Objektreferenz ungültig.
240  
241 =cut
242  
243 # -----------------------------------------------------------------------------
244  
245 100 ▶sub close {
Subroutine close: called CC=1 SCAR=0.0
Pod documented
246 100 $_[0] = undef;
247 100 return;
248 }
249  
250 {
251 270 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
252 270 *destroy = \&close;
253 }
254  
255 # -----------------------------------------------------------------------------
256  
257 =head3 DESTROY() - Schließe Dateihandle
258  
259 =head4 Synopsis
260  
261 $fh->DESTROY;
262  
263 =head4 Description
264  
265 Schließe Dateihandle. Die Methode liefert keinen Wert zurück.
266  
267 =cut
268  
269 # -----------------------------------------------------------------------------
270  
271 114 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.4
272 # warn "DESTROY $_[0]\n";
273 114 ▶ if (Scalar::Util::openhandle($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Scalar::Util::openhandle($_[0])) 113 1 114
274 113 ▶ CORE::close $_[0] or do {
Branch: 1 branch
BranchTrueFalseTotal
CORE::close $_[0] or do { $_[0]->throw("FH-00009: Close of FileHandle failed") } 113 0 113
275 0 $_[0]->throw('FH-00009: Close of FileHandle failed');
276 };
277 }
278 114 return;
279 }
280  
281 # -----------------------------------------------------------------------------
282  
283 =head2 Lesen
284  
285 =head3 read() - Lies Daten von Dateihandle
286  
287 =head4 Synopsis
288  
289 $data = $fh->read($n);
290  
291 =head4 Description
292  
293 Lies die nächste die nächsten $n I<Zeichen> von Dateihandle $fh
294 und liefere diese zurück. Ist das Dateiende erreicht, liefere undef.
295  
296 =cut
297  
298 # -----------------------------------------------------------------------------
299  
300 3 ▶sub read {
Subroutine read: called CC=4 SCAR=16.1
Pod documented
301 3 my ($self,$n) = @_;
302  
303 3 ▶ if ($n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0) 0 3 3
304 # Der Returnwert 0 von read() zeigt nur dann EOF an, wenn
305 # mehr als 0 Bytes gelesen werden sollen
306 0 return '';
307 }
308  
309 3 undef $!;
310 3 $n = CORE::read($self,my $data,$n);
311 3 ▶ if (!defined $n) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $n) { } 0 3 3
elsif ($n == 0) { } 0 3 3
312 0 $self->throw('FH-00012: Read fehlgeschlagen',Errstr=>$!);
313 }
314 elsif ($n == 0) {
315 0 return undef;
316 }
317  
318 3 return $data;
319 }
320  
321 # -----------------------------------------------------------------------------
322  
323 =head3 readData() - Lies Daten mit Längenangabe
324  
325 =head4 Synopsis
326  
327 $data = $fh->readData;
328  
329 =head4 Description
330  
331 Lies Daten in der Repräsentation
332  
333 <LENGTH><DATA>
334  
335 und liefere <DATA> zurück. <LENGTH> ist ein 32 Bit Integer und <DATA>
336 sind beliebige Daten mit <LENGTH> Bytes Länge.
337  
338 Wurden die Daten in einem Encoding wie UTF-8 geschrieben, müssen diese
339 nach dem Einlesen anscheinend nicht dekodiert werden. Warum?
340  
341 Wurden die Daten $data in einem Encoding wie UTF-8 geschrieben, müssen
342 diese anschließend decodiert werden mit
343  
344 Encode::decode('utf-8',$data);
345  
346 Auf der FileHandle $fh das Encoding zu definieren, ist I<nicht>
347 richtig, da die Längenangabe diesem Encoding nicht unterliegt!
348  
349 =head4 See Also
350  
351 writeData()
352  
353 =cut
354  
355 # -----------------------------------------------------------------------------
356  
357 1 ▶sub readData {
Subroutine readData: called CC=2 SCAR=7.4
Pod documented
358 1 my $self = shift;
359  
360 1 my $length = $self->read(4);
361 1 ▶ if (!defined $length) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $length) 0 1 1
362 0 return undef;
363 }
364  
365 1 return $self->read(unpack 'I',$length);
366 }
367  
368 # -----------------------------------------------------------------------------
369  
370 =head3 readLine() - Lies Zeile von Dateihandle
371  
372 =head4 Synopsis
373  
374 $line = $fh->readLine;
375  
376 =head4 Description
377  
378 Lies die nächste Zeile von Dateihandle $fh und liefere diese zurück.
379 Schlägt dies fehl, wirf eine Exception.
380  
381 =cut
382  
383 # -----------------------------------------------------------------------------
384  
385 7 ▶sub readLine {
Subroutine readLine: called CC=3 SCAR=11.6
Pod documented
386 7 my $self = shift;
387  
388 7 undef $!;
389 7 my $line = CORE::readline $self;
390 7 ▶ if (!defined $line and $!) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $line and $!) 0 7 7
Condition66%2/3
not defined $line and $!
!ll&&!rl&&r
610
MC/DC0%0/2
not defined $line and $!
not defined $line $!
Truth table66%2/3
not(A) and B
A= not defined $lineB= $!
ABresult
0X0
100
111
391 0 $self->throw('FH-00011: ReadLine fehlgeschlagen',Errstr=>$!);
392 }
393  
394 7 return $line;
395 }
396  
397 # -----------------------------------------------------------------------------
398  
399 =head3 readLines() - Lies mehrere Zeilen von Dateihandle
400  
401 =head4 Synopsis
402  
403 @lines|$lineA = $fh->readLines($n);
404  
405 =head4 Description
406  
407 Lies die nächsten $n Zeilen von Dateihandle $fh und liefere diese als
408 Liste zurück. Der Zeilentrenner am Ende jeder Zeile wird entfernt.
409 Im Skalarkontext liefere eine Referenz auf die Liste, wenn
410 Zeilen gelesen wurden, sonst C<undef>.
411  
412 =head4 Example
413  
414 Liefere Chunks von 1000 Pfaden:
415  
416 my $fh = Quiq::FileHandle->new('-|',"find @$dirA -name '*.xml.gz'");
417 while (my $fileA = $fh->readLines(1000)) {
418 ...
419 }
420 $fh->close;
421  
422 =cut
423  
424 # -----------------------------------------------------------------------------
425  
426 0 ▶sub readLines {
Subroutine readLines: not called CC=5 SCAR=34.0
Pod documented
427 0 my ($self,$n) = @_;
428  
429 0 my @lines;
430 0 ▶ for (my $i = 0; $i < $n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
431 0 my $line = <$self>;
432 0 ▶ if (!defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 0 0 0
433 0 last;
434 }
435 0 chomp $line;
436 0 push @lines,$line;
437 }
438  
439 0 ▶ return wantarray? @lines: @lines? \@lines: undef;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 0 0 0
@lines ? : 0 0 0
440 }
441  
442 # -----------------------------------------------------------------------------
443  
444 =head3 readLineChomp() - Lies Zeile ohne Zeilentrenner von Dateihandle
445  
446 =head4 Synopsis
447  
448 $line = $fh->readLineChomp;
449  
450 =head4 Description
451  
452 Lies die nächste Zeile von Dateihandle $fh, entferne den Zeilentrenner
453 mit chomp() und liefere das Resultat zurück.
454  
455 =cut
456  
457 # -----------------------------------------------------------------------------
458  
459 0 ▶sub readLineChomp {
Subroutine readLineChomp: not called CC=2 SCAR=17.9
Pod documented
460 0 my $self = shift;
461  
462 0 my $line = $self->readLine;
463 0 ▶ if (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 0 0 0
464 0 chomp $line;
465 }
466  
467 0 return $line;
468 }
469  
470 # -----------------------------------------------------------------------------
471  
472 =head3 readLineNoWhiteSpace() - Lies Zeile und entferne Whitespace am Ende
473  
474 =head4 Synopsis
475  
476 $line = $fh->readLineNoWhiteSpace;
477  
478 =head4 Description
479  
480 Lies die nächste Zeile von Dateihandle $fh, entferne
481 jeglichen Whitespace am Zeilenende (mit s/\s+$//) und liefere
482 das Resultat zurück.
483  
484 Diese Funktion ist nützlich, wenn verschiedene Zeilentrenner
485 CRLF oder LF vorkommen können und Zeilen nur mit Whitespace
486 zu Leerzeilen reduziert werden sollen.
487  
488 =cut
489  
490 # -----------------------------------------------------------------------------
491  
492 0 ▶sub readLineNoWhiteSpace {
Subroutine readLineNoWhiteSpace: not called CC=2 SCAR=17.9
Pod documented
493 0 my $self = shift;
494  
495 0 my $line = $self->readLine;
496 0 ▶ if (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 0 0 0
497 0 $line =~ s/\s+$//;
498 }
499  
500 0 return $line;
501 }
502  
503 # -----------------------------------------------------------------------------
504  
505 =head3 getc() - Lies nächstes Zeichen
506  
507 =head4 Synopsis
508  
509 $c = $fh->getc;
510  
511 =cut
512  
513 # -----------------------------------------------------------------------------
514  
515 1 ▶sub getc {
Subroutine getc: called CC=3 SCAR=12.3
Pod documented
516 1 my $self = shift;
517  
518 # MEMO: 'Bad file descriptor' kommt anscheinend immer am Dateiende
519
520 1 undef $!;
521 1 my $c = CORE::getc($self);
522 1 ▶ if (!defined($c) && $! ne 'Bad file descriptor') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $c and $! ne "Bad file descriptor") 0 1 1
Condition33%1/3
not defined $c and $! ne "Bad file descriptor"
!ll&&!rl&&r
100
MC/DC0%0/2
not defined $c and $! ne "Bad file descriptor"
not defined $c $! ne "Bad file descriptor"
Truth table33%1/3
not(A) and B
A= not defined $cB= $! ne "Bad file descriptor"
ABresult
0X0
100
111
523 0 $self->throw('FH-00011: getc() fehlgeschlagen',Errstr=>$!);
524 }
525  
526 1 return $c;
527 }
528  
529 # -----------------------------------------------------------------------------
530  
531 =head3 slurp() - Lies Rest der Datei
532  
533 =head4 Synopsis
534  
535 $data = $fh->slurp;
536  
537 =head4 Returns
538  
539 String
540  
541 =head4 Description
542  
543 Lies den Rest von Dateihandle $fh liefere diesen zurück.
544  
545 Die Methode ist nützlich, wenn der gesamte Inhalt einer Datei ab einer
546 bestimmten Position gelesen werden soll.
547  
548 =head4 Example
549  
550 Lies gesamten Inhalt einer Datei ab Position $pos:
551  
552 my $fh = Quiq::FileHandle->open('<',$logFile);
553 $fh->seek($pos);
554 my $data = $fh->slurp;
555  
556 =cut
557  
558 # -----------------------------------------------------------------------------
559  
560 2 ▶sub slurp {
Subroutine slurp: called CC=1 SCAR=0.0
Pod documented
561 2 my $self = shift;
562 2 local $/;
563 2 return <$self>;
564 }
565  
566 # -----------------------------------------------------------------------------
567  
568 =head2 Schreiben
569  
570 =head3 print() - Schreibe Daten auf Dateihandle
571  
572 =head4 Synopsis
573  
574 $fh->print(@data);
575  
576 =head4 Alias
577  
578 write()
579  
580 =head4 Description
581  
582 Schreibe Daten @data auf Dateihandle $fh. Die Methode liefert
583 keinen Wert zurück.
584  
585 =cut
586  
587 # -----------------------------------------------------------------------------
588  
589 21 ▶sub print {
Subroutine print: called CC=1 SCAR=0.0
Pod documented
590 21 my $self = shift;
591 # @_: @data
592 21 Quiq::Perl->print($self,@_);
593 21 return;
594 }
595  
596 {
597 270 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
598 270 *write = \&print;
599 }
600  
601 # -----------------------------------------------------------------------------
602  
603 =head3 writeData() - Schreibe Daten mit Längenangabe
604  
605 =head4 Synopsis
606  
607 $fh->writeData($data);
608  
609 =head4 Description
610  
611 Schreibe die Daten $data in der Repräsentation
612  
613 <LENGTH><DATA>
614  
615 Hierbei ist <LENGTH> ein 32 Bit Integer, der die Länge der
616 darauffolgenden Daten <DATA> in Bytes angibt.
617  
618 Liegen die Daten $data in einem Encoding wie UTF-8 vor, müssen diese
619 zuvor encodiert werden mit
620  
621 Encode::encode('utf-8',$data);
622  
623 Auf der FileHandle $fh das Encoding zu definieren, ist I<nicht>
624 richtig, da die Längenangabe diesem Encoding nicht unterliegt!
625  
626 =head4 See Also
627  
628 readData()
629  
630 =cut
631  
632 # -----------------------------------------------------------------------------
633  
634 1 ▶sub writeData {
Subroutine writeData: called CC=1 SCAR=0.0
Pod documented
635 1 my ($self,$str) = @_;
636 1 Quiq::Perl->print($self,pack('I',bytes::length($str)),$str);
637 1 return;
638 }
639  
640 # -----------------------------------------------------------------------------
641  
642 =head3 truncate() - Kürze Datei
643  
644 =head4 Synopsis
645  
646 $fh->truncate;
647 $fh->truncate($length);
648  
649 =head4 Description
650  
651 Kürze Datei auf Länge $length. Ist $length nicht angegeben, kürze
652 Datei auf Länge 0.
653  
654 =cut
655  
656 # -----------------------------------------------------------------------------
657  
658 7 ▶sub truncate {
Subroutine truncate: called CC=3 SCAR=12.0
Pod documented
659 7 my $self = shift;
660 7 ▶ my $length = shift || 0;
Condition50%1/2
shift() || 0
l!l
07
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
661  
662 7 ▶ unless (CORE::truncate $self,$length) {
Branch: 1 branch
BranchTrueFalseTotal
unless (CORE::truncate $self, $length) 0 7 7
663 0 $self->throw(
664 'FH-00013: truncate fehlgeschlagen',
665 Errstr=>$!,
666 );
667 }
668  
669 7 return;
670 }
671  
672 # -----------------------------------------------------------------------------
673  
674 =head2 Positionieren
675  
676 =head3 seek() - Setze Position in Datei
677  
678 =head4 Synopsis
679  
680 $fh->seek($pos);
681 $fh->seek($pos,$whence);
682  
683 =head4 Description
684  
685 Setze die Position der Filehandle in der Datei. Die Methode liefert
686 keinen Wert zurück. Genaue Funktionsbeschreibung siehe
687 Perl-Dokumentation (perldoc -f seek).
688  
689 =cut
690  
691 # -----------------------------------------------------------------------------
692  
693 4 ▶sub seek {
Subroutine seek: called CC=3 SCAR=11.8
Pod documented
694 4 my $self = shift;
695 4 my $pos = shift;
696 4 ▶ my $whence = shift || 0;
Condition50%1/2
shift() || 0
l!l
04
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
697  
698 4 ▶ unless (CORE::seek $self,$pos,$whence) {
Branch: 1 branch
BranchTrueFalseTotal
unless (CORE::seek $self, $pos, $whence) 0 4 4
699 0 $self->throw('FH-00014: seek fehlgeschlagen',Errstr=>$!);
700 }
701  
702 4 return;
703 }
704  
705 # -----------------------------------------------------------------------------
706  
707 =head3 tell() - Liefere Position in Datei
708  
709 =head4 Synopsis
710  
711 $pos = $fh->tell;
712  
713 =head4 Description
714  
715 Liefere die Position der Filehandle in der Datei. Genaue
716 Funktionsbeschreibung siehe Perl-Dokumentation (perldoc -f tell).
717  
718 =cut
719  
720 # -----------------------------------------------------------------------------
721  
722 1 ▶sub tell {
Subroutine tell: called CC=2 SCAR=7.4
Pod documented
723 1 my $self = shift;
724  
725 1 my $pos = CORE::tell $self;
726 1 ▶ if ($pos < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < 0) 0 1 1
727 0 $self->throw('FH-00013: tell fehlgeschlagen',Errstr=>$!);
728 }
729  
730 1 return $pos;
731 }
732  
733 # -----------------------------------------------------------------------------
734  
735 =head2 Sperren
736  
737 =head3 lock() - Sperre Datei
738  
739 =head4 Synopsis
740  
741 $fh->lock($lockMode);
742  
743 =head4 Description
744  
745 Locke die Datei im Lock-Modus $lockMode. Die Methode liefert keinen
746 Wert zurück.
747  
748 Folgende Lockmodes werden unterschieden:
749  
750 =over 4
751  
752 =item 'SH'
753  
754 shared lock
755  
756 =item 'EX'
757  
758 exclusive lock
759  
760 =item 'SHNB'
761  
762 shared lock, non-blocking
763  
764 =item 'EXNB'
765  
766 exclusive lock, non-bloking
767  
768 =back
769  
770 Liefere "wahr", wenn der Lock gesetzt werden kann, im Fehlerfall
771 liefere "falsch".
772  
773 =cut
774  
775 # -----------------------------------------------------------------------------
776  
777 8 ▶sub lock {
Subroutine lock: called CC=6 SCAR=17.9
Pod documented
778 8 my ($self,$lockMode) = @_;
779  
780 8 my $lock;
781 8 ▶ if ($lockMode eq 'SH') {
Branch: 4 branches
BranchTrueFalseTotal
if ($lockMode eq 'SH') { } 1 7 8
elsif ($lockMode eq 'EX') { } 4 3 7
elsif ($lockMode eq 'SHNB') { } 1 2 3
elsif ($lockMode eq 'EXNB') { } 1 1 2
782 1 $lock = Fcntl::LOCK_SH;
783 }
784 elsif ($lockMode eq 'EX') {
785 4 $lock = Fcntl::LOCK_EX;
786 }
787 elsif ($lockMode eq 'SHNB') {
788 1 $lock = Fcntl::LOCK_SH|Fcntl::LOCK_NB;
789 }
790 elsif ($lockMode eq 'EXNB') {
791 1 $lock = Fcntl::LOCK_EX|Fcntl::LOCK_NB;
792 }
793 else {
794 1 $self->throw('FH-00002: Unbekannter Lock-Modus',LockMode=>$lockMode);
795 }
796  
797 7 ▶ return flock($self,$lock)? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
flock($self, $lock) ? : 7 0 7
798 }
799  
800 # -----------------------------------------------------------------------------
801  
802 =head3 unlock() - Hebe Sperre auf
803  
804 =head4 Synopsis
805  
806 $fh->unlock;
807  
808 =head4 Description
809  
810 Hebe Sperre auf Dateihandle $fh auf. Die Methode liefert keinen
811 Wert zurück.
812  
813 =cut
814  
815 # -----------------------------------------------------------------------------
816  
817 1 ▶sub unlock {
Subroutine unlock: called CC=2 SCAR=7.6
Pod documented
818 1 my $self = shift;
819  
820 1 ▶ unless (flock $self,Fcntl::LOCK_UN) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $self, 8) 0 1 1
821 0 $self->throw('FH-00003: Kann Lock nicht aufheben',Errstr=>$!);
822 };
823  
824 1 return;
825 }
826  
827 # -----------------------------------------------------------------------------
828  
829 =head2 Encoding
830  
831 =head3 setEncoding() - Setze Encoding
832  
833 =head4 Synopsis
834  
835 $fh = $fh->setEncoding($encoding);
836  
837 =head4 Arguments
838  
839 =over 4
840  
841 =item $encoding
842  
843 Bezeichnung des Encoding.
844  
845 =back
846  
847 =head4 Returns
848  
849 FileHandle-Objekt (für Method-Chaining)
850  
851 =head4 Description
852  
853 Definiere für Filehandle $fh das Encoding $encoding. D.h. alle Daten
854 werden automatisch gemäß diesem Encoding beim Schreiben encodiert
855 bzw. beim Lesen dekodiert.
856  
857 =cut
858  
859 # -----------------------------------------------------------------------------
860  
861 3 ▶sub setEncoding {
Subroutine setEncoding: called CC=1 SCAR=0.0
Pod documented
862 3 my ($self,$encoding) = @_;
863 3 $self->binmode(sprintf ':encoding(%s)',$encoding);
864 3 return $self;
865 }
866  
867 # -----------------------------------------------------------------------------
868  
869 =head2 Verschiedenes
870  
871 =head3 autoFlush() - Schalte Filehandle in ungepufferten Modus
872  
873 =head4 Synopsis
874  
875 $fh->autoFlush;
876 $fh->autoFlush($bool);
877  
878 =cut
879  
880 # -----------------------------------------------------------------------------
881  
882 2 ▶sub autoFlush {
Subroutine autoFlush: called CC=2 SCAR=7.0
Pod documented
883 2 my $self = shift;
884 2 ▶ my $bool = @_? shift: 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 2 2
885  
886 2 my $tmp = CORE::select $self;
887 2 $| = $bool;
888 2 CORE::select $tmp;
889  
890 2 return;
891 }
892  
893 # -----------------------------------------------------------------------------
894  
895 =head3 binmode() - Aktiviere Binärmodus oder Layer
896  
897 =head4 Synopsis
898  
899 $fh->binmode;
900 $fh->binmode($layer);
901  
902 =head4 Description
903  
904 Schalte Filehandle in Binärmodus oder setze Layer $layer. Genaue
905 Funktionsbeschreibung siehe Perl-Dokumentation (perldoc -f binmode).
906  
907 =cut
908  
909 # -----------------------------------------------------------------------------
910  
911 10 ▶sub binmode {
Subroutine binmode: called CC=1 SCAR=0.0
Pod documented
912 10 Quiq::Perl->binmode(@_);
913 10 return;
914 }
915  
916 # -----------------------------------------------------------------------------
917  
918 =head2 Klassenmethoden
919  
920 =head3 captureStderr() - Fange STDERR ab
921  
922 =head4 Synopsis
923  
924 $class->captureStderr(\$str);
925  
926 =head4 Returns
927  
928 Die Methode liefert keinen Wert zurrück
929  
930 =head4 Description
931  
932 Fange alle Ausgaben auf STDERR ab und lenke sie auf Skalarvariable
933 $str um.
934  
935 =cut
936  
937 # -----------------------------------------------------------------------------
938  
939 0 ▶sub captureStderr {
Subroutine captureStderr: not called CC=3 SCAR=24.8
Pod documented
940 0 my ($class,$ref) = @_;
941  
942 0 ▶ if (Scalar::Util::openhandle(*STDERR)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Scalar::Util::openhandle(*STDERR)) 0 0 0
943 0 CORE::close STDERR;
944 }
945 0 ▶ CORE::open STDERR,'>',$ref or do {
Branch: 1 branch
BranchTrueFalseTotal
CORE::open STDERR, ">", $ref or do { $class->throw("FH-00001: Capture of STDERR failed", "Errstr", $!) } 0 0 0
946 0 $class->throw(
947 'FH-00001: Capture of STDERR failed',
948 Errstr=>$!,
949 );
950 };
951  
952 0 return;
953 }
954  
955 # -----------------------------------------------------------------------------
956  
957 =head3 slurpFromStdin() - Lies Eingaben von STDIN
958  
959 =head4 Synopsis
960  
961 $data = $class->slurpFromStdin;
962  
963 =head4 Returns
964  
965 String
966  
967 =head4 Description
968  
969 Lies alle Eingaben von STDIN und liefere diese als eine
970 Zeichenkette zurück.
971  
972 =cut
973  
974 # -----------------------------------------------------------------------------
975  
976 0 ▶sub slurpFromStdin {
Subroutine slurpFromStdin: not called CC=1 SCAR=6.9
Pod documented
977 0 return shift->new('<')->slurp;
978 }
979  
980 # -----------------------------------------------------------------------------
981  
982 =head1 VERSION
983  
984 1.240
985  
986 =head1 AUTHOR
987  
988 Frank Seitz, L<http://fseitz.de/>
989  
990 =head1 COPYRIGHT
991  
992 Copyright (C) 2026 Frank Seitz
993  
994 =head1 LICENSE
995  
996 This code is free software; you can redistribute it and/or modify
997 it under the same terms as Perl itself.
998  
999 =cut
1000  
1001 # -----------------------------------------------------------------------------
1002  
1003 270 1;
1004  
1005 # eof
« Quiq/Html/Pygments.pm ↑ Summary Quiq/StreamServe/Block.pm »