blib/lib/File/Print/Many.pm

filter: Statementstmt 91.8% 34 / 37 Branchbran 72.2% 13 / 18 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 88.4% 61 / 69 CC 13 SCAR 18.9
CC
13
Cov
86%
new
10.3
print
4.6
BEGIN
1.0
CRAP 6.6
↑ Summary
1 package File::Print::Many;
2  
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 # require Tie::Handle;
9  
10 =head1 NAME
11  
12 File::Print::Many - Print to more than one file descriptor at once
13  
14 =head1 VERSION
15  
16 Version 0.04
17  
18 =cut
19  
20 4 our $VERSION = '0.04';
21 # our @ISA = ('Tie::Handle');
22  
23 =head1 SYNOPSIS
24  
25 Print to more than one file descriptor at once.
26  
27 =head1 SUBROUTINES/METHODS
28  
29 =head2 new
30  
31 use File::Print::Many;
32 open(my $fout1, '>', '/tmp/foo') or die "Cannot open file: $!";
33 open(my $fout2, '>', '/tmp/bar') or die "Cannot open file: $!";
34 my $many = File::Print::Many->new(fds => [$fout1, $fout2]);
35 print $fout1 "this only goes to /tmp/foo\n";
36 $many->print("this goes to both files\n");
37  
38 =cut
39  
40 sub new
41 13 ▶{
Subroutine new: called CC=10 SCAR=23.3
Pod documented
42 13 my ($class, @args) = @_;
43  
44 13 ▶ Carp::croak('Usage: new(fds => \@array)') unless(defined $class);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 13 13
45  
46 # Handle hash or hashref arguments
47 13 ▶ my %args = ref $args[0] eq 'HASH' ? %{ $args[0] }
Branch: 3 branches
BranchTrueFalseTotal
ref $args[0] eq 'HASH' ? : 5 8 13
ref $args[0] eq 'ARRAY' ? : 1 7 8
scalar(@args) % 2 == 0 ? : 6 1 7
48 : ref $args[0] eq 'ARRAY' ? (fds => $args[0])
49 : (scalar(@args) % 2) == 0 ? @args
50 : Carp::croak('Usage: new(fds => \@array)');
51  
52 # If cloning an object, merge arguments
53 12 ▶ if(Scalar::Util::blessed($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($class)) 0 12 12
54 0 return bless { %$class, %args }, ref($class);
55 }
56  
57 # Validate file descriptor array
58 Carp::croak('Usage: new(fds => \@array)')
59 12 ▶ if(ref($args{'fds'}) ne 'ARRAY') || (!defined @{$args{fds}}[0]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $args{"fds"} ne "ARRAY" or not defined @{$args{"fds"};}[0] 7 5 12
Condition100%3/3
ref $args{"fds"} ne "ARRAY" or not defined @{$args{"fds"};}[0]
l!l&&r!l&&!r
615
MC/DC100%2/2
ref $args{"fds"} ne "ARRAY" or not defined @{$args{"fds"};}[0]
ref $args{"fds"} ne "ARRAY" not defined @{$args{"fds"};}[0]
Truth table100%3/3
A or not(B)
A= ref $args{"fds"} ne "ARRAY"B= not defined @{$args{"fds"};}[0]
ABresult
1X1
011
000
60  
61 # Ensure all elements in fds are valid filehandles
62 5 foreach my $fd (@{$args{fds}}) {
63 10 ▶ Carp::croak('Invalid filehandle') unless(defined fileno($fd));
Branch: 1 branch
BranchTrueFalseTotal
unless defined fileno $fd 0 10 10
64 }
65  
66 # Create the object
67 5 return bless { _fds => $args{fds} }, $class;
68 }
69  
70 =head2 print
71  
72 Send output.
73  
74 $many->print("hello, world!\n");
75 $many->print('hello, ', "world!\n");
76 $many->print('hello, ')->print("world!\n");
77  
78 =cut
79  
80 sub print
81 7 ▶{
Subroutine print: called CC=4 SCAR=15.2
Pod documented
82 7 my ($self, @data) = @_;
83  
84 # Sanity check: Ensure _fds exists and is an array reference
85 7 ▶ unless(ref($self->{'_fds'}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"_fds"} eq "ARRAY") 0 7 7
86 0 Carp::croak("BUG: Invalid file descriptors: '_fds' must be an array reference");
87 }
88  
89 # Print data to each file descriptor
90 7 foreach my $fd(@{$self->{'_fds'}}) {
91 14 ▶ unless(print $fd @data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (print $fd @data) 0 14 14
92 0 Carp::croak("Failed to write to filehandle: $!");
93 }
94 }
95  
96 7 return $self;
97 }
98  
99 # This code would add support for this, but I don't need it
100 # tie *MULTI, 'File::Print::Many', fds => [$fh1, $fh2];
101 # print MULTI "This goes to both files\n";
102  
103 # =head2 TIEHANDLE
104 #
105 # Allows the object to be tied to a filehandle.
106 #
107 # =cut
108 #
109 # sub TIEHANDLE {
110 # my ($class, @args) = @_;
111 # return $class->new(@args);
112 # }
113 #
114 # =head2 PRINT
115 #
116 # Handles the 'print' operation when tied to a filehandle.
117 #
118 # =cut
119 #
120 # sub PRINT {
121 # my $self = shift;
122 # $self->print(@_);
123 # }
124  
125 =head1 AUTHOR
126  
127 Nigel Horne, C<< <njh at bandsman.co.uk> >>
128  
129 =head1 BUGS
130  
131 This module is provided as-is without any warranty.
132  
133 Please report any bugs or feature requests to C<bug-file-print-many at rt.cpan.org>,
134 or through the web interface at
135 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Print-Many>.
136 I will be notified, and then you'll
137 automatically be notified of progress on your bug as I make changes.
138  
139 =head1 SEE ALSO
140  
141 =head1 SUPPORT
142  
143 You can find documentation for this module with the perldoc command.
144  
145 perldoc File::Print::Many
146  
147 You can also look for information at:
148  
149 =over 4
150  
151 =item * RT: CPAN's request tracker
152  
153 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Print-Many>
154  
155 =item * AnnoCPAN: Annotated CPAN documentation
156  
157 L<http://annocpan.org/dist/File-Print-Many>
158  
159 =back
160  
161 =head1 LICENCE AND COPYRIGHT
162  
163 Copyright 2018-2025 Nigel Horne.
164  
165 This program is released under the following licence: GPL2
166  
167 =cut
168  
169 4 1;
↑ Summary