File: Prototype.pm

package info (click to toggle)
libio-handle-util-perl 0.02-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 320 kB
  • sloc: perl: 1,353; makefile: 2
file content (88 lines) | stat: -rw-r--r-- 1,662 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package IO::Handle::Prototype;

use strict;
use warnings;

our $VERSION = '0.02';

use Carp ();

use parent qw(IO::Handle::Util::Overloading);

sub new {
    my ( $class, @args ) = @_;

    my $cb = @args == 1 ? $args[0] : {@args};

    bless {
        cb => $cb,
    }, $class;
}

sub _cb {
    my $self = shift;
    my $name = shift;

    if ( my $cb = $self->{cb}{$name} ) {
        return $self->$cb(@_);
    } else {
        Carp::croak("No implementation of '$name' provided for $self");
    }
}

sub open { shift->_cb(open => @_) }

sub getline { shift->_cb(getline => @_) }
sub getlines { shift->_cb(getlines => @_) }
sub read { shift->_cb(read => @_) }
sub sysread { shift->_cb(sysread => @_) }
sub getc { shift->_cb(getc => @_) }
sub ungetc { shift->_cb(ungetc => @_) }

sub say { shift->_cb(say => @_) }
sub print { shift->_cb(print => @_) }
sub printf { shift->_cb(printf => @_) }

sub format_write { shift->_cb(format_write => @_) }
sub write { shift->_cb(write => @_) }
sub syswrite { shift->_cb(syswrite => @_) }

sub ioctl { shift->_cb(ioctl => @_) }
sub fcntl { shift->_cb(fcntl => @_) }

sub truncate { shift->_cb(truncate => @_) }

sub stat { shift->_cb(stat => @_) }
sub fileno { shift->_cb(fileno => @_) }

sub eof { shift->_cb(eof => @_) }

sub close { shift->_cb(close => @_) }

__PACKAGE__

# ex: set sw=4 et:

__END__

=pod

=head1 NAME

IO::Handle::Prototype - base class for callback based handles.

=head1 SYNOPSIS

    my $fh = IO::Handle::Prototype->new(
        getline => sub {
            my $fh = shift;

            ...
        },
    );

=head1 DESCRIPTION

You probably want L<IO::Handle::Prototype::Fallback> instead.

=cut