1 package Test::Valgrind::Action::Captor;
8 Test::Valgrind::Action::Captor - Mock Test::Valgrind::Action for capturing output.
16 our $VERSION = '1.17';
20 This class provides helpers for saving, redirecting and restoring filehandles.
22 It's not meant to be used directly as an action.
28 use base qw<Test::Valgrind::Carp>;
34 Just a croaking stub to remind you not to use this class as a real action.
38 sub new { shift->_croak('This mock action isn\'t meant to be used directly') }
40 # Widely inspired from Capture::Tiny
43 open $_[1], $_[2], $_[3]
44 or $_[0]->_croak('open(' . fileno($_[1]) . ", '$_[2]', '$_[3]'): $!");
48 my $fd = fileno $_[3];
49 open $_[1], $_[2] . '&' . $fd
50 or $_[0]->_croak('open(' . fileno($_[1]) . ", '$_[2]&', $fd): $!");
55 $tva->save_fh($from, $mode);
56 $tva->save_fh($from, $mode, $to);
58 Save the original filehandle C<$from> opened with mode C<$mode>, and redirect it to C<$to> if it's defined or to F</dev/null> otherwise.
63 my ($self, $from, $mode, $to) = @_;
65 unless (defined fileno $from) {
66 $self->_redirect_fh($from, $mode, File::Spec->devnull);
67 push @{$self->{proxies}}, $from;
70 $self->_dup_fh(my $save, $mode, $from);
71 push @{$self->{saves}}, [ $save, $mode, $from ];
73 if ($to and ref $to eq 'GLOB') {
74 $self->_dup_fh($from, $mode, $to);
76 $self->_redirect_fh($from, $mode, defined $to ? $to : File::Spec->devnull);
82 =head2 C<restore_all_fh>
86 Restore all the filehandles that were saved with L</save_fh> to their original state.
88 The redirections aren't closed.
95 for (@{$self->{saves}}) {
96 my ($save, $mode, $from) = @$_;
97 $self->_dup_fh($from, $mode, $save);
98 close $save or $self->_croak('close(saved[' . fileno($save) . "]): $!");
100 delete $self->{saves};
102 for (@{$self->{proxies}}) {
103 close $_ or $self->_croak('close(proxy[' . fileno($_) . "]): $!");
105 delete $self->{proxies};
112 L<Test::Valgrind>, L<Test::Valgrind::Action>.
118 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
120 You can contact me by mail or on C<irc.perl.org> (vincent).
124 Please report any bugs or feature requests to C<bug-test-valgrind at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Valgrind>.
125 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
129 You can find documentation for this module with the perldoc command.
131 perldoc Test::Valgrind::Action::Captor
133 =head1 COPYRIGHT & LICENSE
135 Copyright 2009,2010,2011,2013,2015 Vincent Pit, all rights reserved.
137 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
141 1; # End of Test::Valgrind::Action::Captor