Merge branch 'master' of http://repo.or.cz/r/msysgit into devel
[msysgit/historical-msysgit.git] / lib / perl5 / 5.6.1 / SelectSaver.pm
blob5f569222fcc5d0f497810d3b0baf19249d650946
1 package SelectSaver;
3 =head1 NAME
5 SelectSaver - save and restore selected file handle
7 =head1 SYNOPSIS
9 use SelectSaver;
12 my $saver = new SelectSaver(FILEHANDLE);
13 # FILEHANDLE is selected
15 # previous handle is selected
18 my $saver = new SelectSaver;
19 # new handle may be selected, or not
21 # previous handle is selected
23 =head1 DESCRIPTION
25 A C<SelectSaver> object contains a reference to the file handle that
26 was selected when it was created. If its C<new> method gets an extra
27 parameter, then that parameter is selected; otherwise, the selected
28 file handle remains unchanged.
30 When a C<SelectSaver> is destroyed, it re-selects the file handle
31 that was selected when it was created.
33 =cut
35 require 5.000;
36 use Carp;
37 use Symbol;
39 sub 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;
47 sub DESTROY {
48 my $this = $_[0];
49 select $$this[0];