| 1 | package IO::Wrap; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Exporter; | ||
| 5 | use FileHandle; | ||
| 6 | use Carp; | ||
| 7 | |||
| 8 | our $VERSION = '2.113'; | ||
| 9 | our @ISA = qw(Exporter); | ||
| 10 | our @EXPORT = qw(wraphandle); | ||
| 11 | |||
| 12 | |||
| 13 | #------------------------------ | ||
| 14 | # wraphandle RAW | ||
| 15 | #------------------------------ | ||
| 16 | sub wraphandle { | ||
| 17 | my $raw = shift; | ||
| 18 | new IO::Wrap $raw; | ||
| 19 | } | ||
| 20 | |||
| 21 | #------------------------------ | ||
| 22 | # new STREAM | ||
| 23 | #------------------------------ | ||
| 24 | sub new { | ||
| 25 | my ($class, $stream) = @_; | ||
| 26 | no strict 'refs'; | ||
| 27 | |||
| 28 | ### Convert raw scalar to globref: | ||
| 29 | ref($stream) or $stream = \*$stream; | ||
| 30 | |||
| 31 | ### Wrap globref and incomplete objects: | ||
| 32 | if ((ref($stream) eq 'GLOB') or ### globref | ||
| 33 | (ref($stream) eq 'FileHandle') && !defined(&FileHandle::read)) { | ||
| 34 | return bless \$stream, $class; | ||
| 35 | } | ||
| 36 | $stream; ### already okay! | ||
| 37 | } | ||
| 38 | |||
| 39 | #------------------------------ | ||
| 40 | # I/O methods... | ||
| 41 | #------------------------------ | ||
| 42 | sub close { | ||
| 43 | my $self = shift; | ||
| 44 | return close($$self); | ||
| 45 | } | ||
| 46 | sub fileno { | ||
| 47 | my $self = shift; | ||
| 48 | my $fh = $$self; | ||
| 49 | return fileno($fh); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub getline { | ||
| 53 | my $self = shift; | ||
| 54 | my $fh = $$self; | ||
| 55 | return scalar(<$fh>); | ||
| 56 | } | ||
| 57 | sub getlines { | ||
| 58 | my $self = shift; | ||
| 59 | wantarray or croak("Can't call getlines in scalar context!"); | ||
| 60 | my $fh = $$self; | ||
| 61 | <$fh>; | ||
| 62 | } | ||
| 63 | sub print { | ||
| 64 | my $self = shift; | ||
| 65 | print { $$self } @_; | ||
| 66 | } | ||
| 67 | sub read { | ||
| 68 | my $self = shift; | ||
| 69 | return read($$self, $_[0], $_[1]); | ||
| 70 | } | ||
| 71 | sub seek { | ||
| 72 | my $self = shift; | ||
| 73 | return seek($$self, $_[0], $_[1]); | ||
| 74 | } | ||
| 75 | sub tell { | ||
| 76 | my $self = shift; | ||
| 77 | return tell($$self); | ||
| 78 | } | ||
| 79 | |||
| 80 | 1; | ||
| 81 | __END__ |