/usr/share/perl5/IO/All/Base.pm is in libio-all-perl 0.87-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | use strict; use warnings;
package IO::All::Base;
use Fcntl;
sub import {
my $class = shift;
my $flag = $_[0] || '';
my $package = caller;
no strict 'refs';
if ($flag eq '-base') {
push @{$package . "::ISA"}, $class;
*{$package . "::$_"} = \&$_
for qw'field const option chain proxy proxy_open';
}
elsif ($flag eq -mixin) {
mixin_import(scalar(caller(0)), $class, @_);
}
else {
my @flags = @_;
for my $export (@{$class . '::EXPORT'}) {
*{$package . "::$export"} = $export eq 'io'
? $class->_generate_constructor(@flags)
: \&{$class . "::$export"};
}
}
}
sub _generate_constructor {
my $class = shift;
my (@flags, %flags, $key);
for (@_) {
if (s/^-//) {
push @flags, $_;
$flags{$_} = 1;
$key = $_;
}
else {
$flags{$key} = $_ if $key;
}
}
my $constructor;
$constructor = sub {
my $self = $class->new(@_);
for (@flags) {
$self->$_($flags{$_});
}
$self->_constructor($constructor);
return $self;
}
}
sub _init {
my $self = shift;
$self->io_handle(undef);
$self->is_open(0);
return $self;
}
#===============================================================================
# Closure generating functions
#===============================================================================
sub option {
my $package = caller;
my ($field, $default) = @_;
$default ||= 0;
field("_$field", $default);
no strict 'refs';
*{"${package}::$field"} =
sub {
my $self = shift;
*$self->{"_$field"} = @_ ? shift(@_) : 1;
return $self;
};
}
sub chain {
my $package = caller;
my ($field, $default) = @_;
no strict 'refs';
*{"${package}::$field"} =
sub {
my $self = shift;
if (@_) {
*$self->{$field} = shift;
return $self;
}
return $default unless exists *$self->{$field};
return *$self->{$field};
};
}
sub field {
my $package = caller;
my ($field, $default) = @_;
no strict 'refs';
return if defined &{"${package}::$field"};
*{"${package}::$field"} =
sub {
my $self = shift;
unless (exists *$self->{$field}) {
*$self->{$field} =
ref($default) eq 'ARRAY' ? [] :
ref($default) eq 'HASH' ? {} :
$default;
}
return *$self->{$field} unless @_;
*$self->{$field} = shift;
};
}
sub const {
my $package = caller;
my ($field, $default) = @_;
no strict 'refs';
return if defined &{"${package}::$field"};
*{"${package}::$field"} = sub { $default };
}
sub proxy {
my $package = caller;
my ($proxy) = @_;
no strict 'refs';
return if defined &{"${package}::$proxy"};
*{"${package}::$proxy"} =
sub {
my $self = shift;
my @return = $self->io_handle->$proxy(@_);
$self->_error_check;
wantarray ? @return : $return[0];
};
}
sub proxy_open {
my $package = caller;
my ($proxy, @args) = @_;
no strict 'refs';
return if defined &{"${package}::$proxy"};
my $method = sub {
my $self = shift;
$self->_assert_open(@args);
my @return = $self->io_handle->$proxy(@_);
$self->_error_check;
wantarray ? @return : $return[0];
};
*{"$package\::$proxy"} =
(@args and $args[0] eq '>') ?
sub {
my $self = shift;
$self->$method(@_);
return $self;
}
: $method;
}
sub mixin_import {
my $target_class = shift;
$target_class = caller(0)
if $target_class eq 'mixin';
my $mixin_class = shift
or die "Nothing to mixin";
eval "require $mixin_class";
my $pseudo_class = CORE::join '-', $target_class, $mixin_class;
my %methods = mixin_methods($mixin_class);
no strict 'refs';
no warnings;
@{"$pseudo_class\::ISA"} = @{"$target_class\::ISA"};
@{"$target_class\::ISA"} = ($pseudo_class);
for (keys %methods) {
*{"$pseudo_class\::$_"} = $methods{$_};
}
}
sub mixin_methods {
my $mixin_class = shift;
no strict 'refs';
my %methods = all_methods($mixin_class);
map {
$methods{$_}
? ($_, \ &{"$methods{$_}\::$_"})
: ($_, \ &{"$mixin_class\::$_"})
} (keys %methods);
}
sub all_methods {
no strict 'refs';
my $class = shift;
my %methods = map {
($_, $class)
} grep {
defined &{"$class\::$_"} and not /^_/
} keys %{"$class\::"};
return (%methods);
}
1;
|