File Coverage

blib/lib/File/Print/Many.pm
Criterion Covered Total %
statement 32 35 91.4
branch 13 18 72.2
condition 3 3 100.0
subroutine 7 7 100.0
pod 2 2 100.0
total 57 65 87.6


line stmt bran cond sub pod time code
1             package File::Print::Many;
2              
3 4     4   1251750 use warnings;
  4         9  
  4         316  
4 4     4   25 use strict;
  4         8  
  4         121  
5 4     4   21 use Carp;
  4         8  
  4         273  
6 4     4   24 use Scalar::Util;
  4         7  
  4         196  
7 4     4   3104 use namespace::autoclean;
  4         89468  
  4         20  
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             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             {
42 13     13 1 244210 my ($class, @args) = @_;
43              
44 13 50       62 Carp::croak('Usage: new(fds => \@array)') unless(defined $class);
45              
46             # Handle hash or hashref arguments
47 13 100       57 my %args = ref $args[0] eq 'HASH' ? %{ $args[0] }
  5 100       15  
    100          
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 50       27 if(Scalar::Util::blessed($class)) {
54 0         0 return bless { %$class, %args }, ref($class);
55             }
56              
57             # Validate file descriptor array
58             Carp::croak('Usage: new(fds => \@array)')
59 12 100 100     44 if(ref($args{'fds'}) ne 'ARRAY') || (!defined @{$args{fds}}[0]);
  6         27  
60              
61             # Ensure all elements in fds are valid filehandles
62 5         6 foreach my $fd (@{$args{fds}}) {
  5         11  
63 10 50       26 Carp::croak('Invalid filehandle') unless(defined fileno($fd));
64             }
65              
66             # Create the object
67 5         28 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             {
82 7     7 1 467 my ($self, @data) = @_;
83              
84             # Sanity check: Ensure _fds exists and is an array reference
85 7 50       36 unless(ref($self->{'_fds'}) eq 'ARRAY') {
86 0         0 Carp::croak("BUG: Invalid file descriptors: '_fds' must be an array reference");
87             }
88              
89             # Print data to each file descriptor
90 7         12 foreach my $fd(@{$self->{'_fds'}}) {
  7         11  
91 14 50       79 unless(print $fd @data) {
92 0         0 Carp::croak("Failed to write to filehandle: $!");
93             }
94             }
95              
96 7         18 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<< >>
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,
134             or through the web interface at
135             L.
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
154              
155             =item * AnnoCPAN: Annotated CPAN documentation
156              
157             L
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             1;