| 1 | package Seis::IO::Handle; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use utf8; | ||
| 5 | use 5.018_000; | ||
| 6 | |||
| 7 | sub _new { | ||
| 8 | my ($class, $path) = @_; | ||
| 9 | bless { path => $path }, $class; | ||
| 10 | } | ||
| 11 | |||
| 12 | # return Seis::IO->_new_with_fh($_[0], $fh); | ||
| 13 | sub _new_with_fh { | ||
| 14 | my ($class, $path, $fh) = @_; | ||
| 15 | bless { path => $path, fh => $fh }, $class; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub open { | ||
| 19 | my ($self, $mode) = @_; | ||
| 20 | if ($mode->key eq 'w') { | ||
| 21 | open my $fh, '>', $self->{path} | ||
| 22 | or Seis::Exception::IO->throw("Cannot open $self->{path} for writing: $!"); | ||
| 23 | $self->{fh} = $fh; | ||
| 24 | return $self; | ||
| 25 | } else { | ||
| 26 | ... | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | sub lines { | ||
| 31 | my $self = shift; | ||
| 32 | my $fh = $self->{fh}; | ||
| 33 | my @lines = <$fh>; | ||
| 34 | @lines; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub eof:method { | ||
| 38 | warn CORE::eof($_[0]->{fh}); | ||
| 39 | CORE::eof($_[0]->{fh}); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub get:method { | ||
| 43 | my $self = shift; | ||
| 44 | my $fh = $self->{fh}; | ||
| 45 | $self->{ins}++; | ||
| 46 | my $line = scalar(<$fh>); | ||
| 47 | chop($line); | ||
| 48 | $line; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub print:method { | ||
| 52 | my $self = shift; | ||
| 53 | print {$self->{fh}} shift; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub close:method { | ||
| 57 | my $self = shift; | ||
| 58 | close $self->{fh}; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub getc:method { | ||
| 62 | my $self = shift; | ||
| 63 | getc $self->{fh}; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub ins { | ||
| 67 | my $self = shift; | ||
| 68 | $self->{ins}; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub say:method { | ||
| 72 | my $self = shift; | ||
| 73 | CORE::say {$self->{fh}} @_; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub write:method { | ||
| 77 | my $self = shift; | ||
| 78 | CORE::print {$self->{fh}} @_; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub read:method { | ||
| 82 | my ($self, $len) = @_; | ||
| 83 | CORE::read($self->{fh}, my $buf, $len); | ||
| 84 | return $buf; | ||
| 85 | } | ||
| 86 | |||
| 87 | # Note. Following file testing cases are optimizable by XS. | ||
| 88 | sub e { -e $_[0]->{path} ? Bool::true() : Bool::false() } | ||
| 89 | sub d { -d $_[0]->{path} ? Bool::true() : Bool::false() } | ||
| 90 | sub s { -s $_[0]->{path} } # size. | ||
| 91 | sub f { -f $_[0]->{path} ? Bool::true() : Bool::false() } | ||
| 92 | |||
| 93 | sub copy { | ||
| 94 | require File::Copy; | ||
| 95 | File::Copy::copy($_[0]->{path}, $_[1]) | ||
| 96 | or Seis::Exception::IO->throw("$!"); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub isa { | ||
| 100 | my ($self, $stuff) = @_; | ||
| 101 | return $stuff->{name} eq 'IO::Handle' || UNIVERSAL::isa($self, $stuff->{name}) if UNIVERSAL::isa($stuff, 'Seis::Class'); | ||
| 102 | return UNIVERSAL::isa($self, $stuff); | ||
| 103 | } | ||
| 104 | |||
| 105 | 1; | ||
| 106 |