source: trunk/third/perl/lib/SelectSaver.pm @ 10724

Revision 10724, 1.0 KB checked in by ghudson, 27 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r10723, which included commits to RCS files with non-trunk default branches.
Line 
1package SelectSaver;
2
3=head1 NAME
4
5SelectSaver - save and restore selected file handle
6
7=head1 SYNOPSIS
8
9    use SelectSaver;
10
11    {
12       my $saver = new SelectSaver(FILEHANDLE);
13       # FILEHANDLE is selected
14    }
15    # previous handle is selected
16
17    {
18       my $saver = new SelectSaver;
19       # new handle may be selected, or not
20    }
21    # previous handle is selected
22
23=head1 DESCRIPTION
24
25A C<SelectSaver> object contains a reference to the file handle that
26was selected when it was created.  If its C<new> method gets an extra
27parameter, then that parameter is selected; otherwise, the selected
28file handle remains unchanged.
29
30When a C<SelectSaver> is destroyed, it re-selects the file handle
31that was selected when it was created.
32
33=cut
34
35require 5.000;
36use Carp;
37use Symbol;
38
39sub new {
40    @_ >= 1 && @_ <= 2 or croak 'usage: new SelectSaver [FILEHANDLE]';
41    my $fh = select;
42    my $self = bless [$fh], $_[0];
43    select qualify($_[1], caller) if @_ > 1;
44    $self;
45}
46
47sub DESTROY {
48    my $this = $_[0];
49    select $$this[0];
50}
51
521;
Note: See TracBrowser for help on using the repository browser.