1N/Apackage SelectSaver;
1N/A
1N/Aour $VERSION = '1.00';
1N/A
1N/A=head1 NAME
1N/A
1N/ASelectSaver - save and restore selected file handle
1N/A
1N/A=head1 SYNOPSIS
1N/A
1N/A use SelectSaver;
1N/A
1N/A {
1N/A my $saver = new SelectSaver(FILEHANDLE);
1N/A # FILEHANDLE is selected
1N/A }
1N/A # previous handle is selected
1N/A
1N/A {
1N/A my $saver = new SelectSaver;
1N/A # new handle may be selected, or not
1N/A }
1N/A # previous handle is selected
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/AA C<SelectSaver> object contains a reference to the file handle that
1N/Awas selected when it was created. If its C<new> method gets an extra
1N/Aparameter, then that parameter is selected; otherwise, the selected
1N/Afile handle remains unchanged.
1N/A
1N/AWhen a C<SelectSaver> is destroyed, it re-selects the file handle
1N/Athat was selected when it was created.
1N/A
1N/A=cut
1N/A
1N/Arequire 5.000;
1N/Ause Carp;
use Symbol;
sub new {
@_ >= 1 && @_ <= 2 or croak 'usage: new SelectSaver [FILEHANDLE]';
my $fh = select;
my $self = bless [$fh], $_[0];
select qualify($_[1], caller) if @_ > 1;
$self;
}
sub DESTROY {
my $this = $_[0];
select $$this[0];
}
1;