Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.0 KiB

  1. package SelectSaver;
  2. =head1 NAME
  3. SelectSaver - save and restore selected file handle
  4. =head1 SYNOPSIS
  5. use SelectSaver;
  6. {
  7. my $saver = new SelectSaver(FILEHANDLE);
  8. # FILEHANDLE is selected
  9. }
  10. # previous handle is selected
  11. {
  12. my $saver = new SelectSaver;
  13. # new handle may be selected, or not
  14. }
  15. # previous handle is selected
  16. =head1 DESCRIPTION
  17. A C<SelectSaver> object contains a reference to the file handle that
  18. was selected when it was created. If its C<new> method gets an extra
  19. parameter, then that parameter is selected; otherwise, the selected
  20. file handle remains unchanged.
  21. When a C<SelectSaver> is destroyed, it re-selects the file handle
  22. that was selected when it was created.
  23. =cut
  24. require 5.000;
  25. use Carp;
  26. use Symbol;
  27. sub new {
  28. @_ >= 1 && @_ <= 2 or croak 'usage: new SelectSaver [FILEHANDLE]';
  29. my $fh = select;
  30. my $self = bless [$fh], $_[0];
  31. select qualify($_[1], caller) if @_ > 1;
  32. $self;
  33. }
  34. sub DESTROY {
  35. my $this = $_[0];
  36. select $$this[0];
  37. }
  38. 1;