<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package SelectSaver;

our $VERSION = '1.02';

=head1 NAME

SelectSaver - save and restore selected file handle

=head1 SYNOPSIS

    use SelectSaver;

    {
       my $saver = SelectSaver-&gt;new(FILEHANDLE);
       # FILEHANDLE is selected
    }
    # previous handle is selected

    {
       my $saver = SelectSaver-&gt;new;
       # new handle may be selected, or not
    }
    # previous handle is selected

=head1 DESCRIPTION

A C&lt;SelectSaver&gt; object contains a reference to the file handle that
was selected when it was created.  If its C&lt;new&gt; method gets an extra
parameter, then that parameter is selected; otherwise, the selected
file handle remains unchanged.

When a C&lt;SelectSaver&gt; is destroyed, it re-selects the file handle
that was selected when it was created.

=cut

require 5.000;
use Carp;
use Symbol;

sub new {
    @_ &gt;= 1 &amp;&amp; @_ &lt;= 2 or croak 'usage: SelectSaver-&gt;new( [FILEHANDLE] )';
    my $fh = select;
    my $self = bless \$fh, $_[0];
    select qualify($_[1], caller) if @_ &gt; 1;
    $self;
}

sub DESTROY {
    my $self = $_[0];
    select $$self;
}

1;
</pre></body></html>