| 1 | package YATT::Lite::ConnectionProxy; sub MY () {__PACKAGE__} | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 74 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 74 | ▶ | use warnings qw(FATAL all NONFATAL misc); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 74 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 74 | ▶ | use Symbol (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 74 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 74 | ▶ | use Encode (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 74 | require YATT::Lite::Util; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | # $CON while capturing output (GH-295). | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # Like YATT::Lite::Connection, this is a blessed glob. Its IO slot is | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # the capture buffer and its HASH slot holds the original connection. | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # So `print $CON ...` goes to the buffer, and all methods are | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # delegated to the original connection. | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 23 | ▶ | sub capture { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
capture: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 23 | my ($pack, $con, $code) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 23 | ▶ | my $root = _root_connection($con) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | or return &YATT::Lite::Util::captured($code); | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 14 | my $buffer = ""; | |||||||||||||||||||||||||||||||||||||||||
| 23 | { | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 14 | my $glob = Symbol::gensym(); | |||||||||||||||||||||||||||||||||||||||||
| 25 | 14 | ▶ | open $glob, '>:utf8', \ $buffer | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | or die "Can't create capture buf:$!"; | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 14 | bless $glob, $pack; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 14 | *$glob = {connection => $root}; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 14 | $code->($glob); | |||||||||||||||||||||||||||||||||||||||||
| 30 | 14 | close $glob; | |||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 14 | Encode::_utf8_on($buffer); | |||||||||||||||||||||||||||||||||||||||||
| 33 | 14 | $buffer; | |||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | # Nested capture shares the original connection (no proxy of proxy). | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 23 | ▶ | sub _root_connection { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_root_connection: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 23 | my ($con) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 23 | ▶ | return undef unless Scalar::Util::blessed($con); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 14 | ▶ | if (UNIVERSAL::isa($con, MY)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 2 | $con->connection; | |||||||||||||||||||||||||||||||||||||||||
| 42 | } elsif ($con->isa('YATT::Lite::Connection')) { | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 12 | $con; | |||||||||||||||||||||||||||||||||||||||||
| 44 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | undef; | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 22 | ▶ | sub connection { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
connection: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 22 | my ($glob) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 22 | *{$glob}{HASH}->{connection}; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | # Forwarders are independent from the class of the original connection, | ||||||||||||||||||||||||||||||||||||||||||
| 55 | # so they are installed once and shared. | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 74 | our %FORWARDER; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 5 | ▶ | sub _forwarder { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_forwarder: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 5 | my ($name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 5 | ▶ | $FORWARDER{$name} //= do { | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$YATT::Lite::ConnectionProxy::FORWARDER{$name} //= do {
my $sub = sub {
my $glob = shift();
$glob->connection->$name(@_);
}
;
no strict 'refs';
*{'YATT::Lite::ConnectionProxy' . "::$name";} = $sub
}
MC/DC0%0/2
$YATT::Lite::ConnectionProxy::FORWARDER{$name} //= do {
my $sub = sub {
my $glob = shift();
$glob->connection->$name(@_);
}
;
no strict 'refs';
*{'YATT::Lite::ConnectionProxy' . "::$name";} = $sub
}
$YATT::Lite::ConnectionProxy::FORWARDER{$name}
do {
my $sub = sub {
my $glob = shift();
$glob->connection->$name(@_);
}
;
no strict 'refs';
*{'YATT::Lite::ConnectionProxy' . "::$name";} = $sub
}
Truth table66%2/3
A //= B
A= $YATT::Lite::ConnectionProxy::FORWARDER{$name}B= do {
my $sub = sub {
my $glob = shift();
$glob->connection->$name(@_);
}
;
no strict 'refs';
*{'YATT::Lite::ConnectionProxy' . "::$name";} = $sub
}
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 8 | ▶ | my $sub = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 8 | my $glob = shift; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 8 | $glob->connection->$name(@_); | |||||||||||||||||||||||||||||||||||||||||
| 63 | 4 | }; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 74 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 4 | *{MY . "::$name"} = $sub; | |||||||||||||||||||||||||||||||||||||||||
| 66 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 5 | \&{MY . "::$name"}; | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 74 | our $AUTOLOAD; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 5 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=6 SCAR=19.6
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 5 | my $name = substr($AUTOLOAD, rindex($AUTOLOAD, ':') + 1); | |||||||||||||||||||||||||||||||||||||||||
| 73 | 5 | ▶ | my $con = ref $_[0] ? $_[0]->connection : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 5 | ▶ | unless ($con and $con->can($name)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$con and $con->can($name)
MC/DC50%1/2
$con and $con->can($name)
$con
$con->can($name)
Truth table66%2/3
A and B
A= $conB= $con->can($name)
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | ▶ | Carp::croak(sprintf q{Can't locate object method "%s" via package "%s"} | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
ref $con || ref $_[0] || $_[0]
ref $con or ref $_[0]
MC/DC0%0/3
ref $con || ref $_[0] || $_[0]
ref $con
ref $_[0]
$_[0]
Truth table25%1/4
A or B || C
A= ref $conB= ref $_[0]C= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | , $name, ref $con || ref $_[0] || $_[0]); | ||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 4 | goto &{_forwarder($name)}; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | # Methods from ->can must work with the proxy as invocant. | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 4 | ▶ | sub can { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
can: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 4 | my ($glob, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 4 | ▶ | if (not $FORWARDER{$name} and my $sub = $glob->SUPER::can($name)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $YATT::Lite::ConnectionProxy::FORWARDER{$name} and my $sub = $glob->SUPER::can($name)
MC/DC100%2/2
not $YATT::Lite::ConnectionProxy::FORWARDER{$name} and my $sub = $glob->SUPER::can($name)
not $YATT::Lite::ConnectionProxy::FORWARDER{$name}
my $sub = $glob->SUPER::can($name)
Truth table100%3/3
not(A) and B
A= not $YATT::Lite::ConnectionProxy::FORWARDER{$name}B= my $sub = $glob->SUPER::can($name)
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 1 | return $sub; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 3 | ▶ | return undef unless ref $glob; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 3 | my $con = $glob->connection; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 3 | ▶ | $con && $con->can($name) ? _forwarder($name) : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$con and $con->can($name)
MC/DC50%1/2
$con and $con->can($name)
$con
$con->can($name)
Truth table66%2/3
A and B
A= $conB= $con->can($name)
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 3 | ▶ | sub isa { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
isa: called CC=4 SCAR=14.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 3 | my ($glob, $class) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 94 | $glob->SUPER::isa($class) | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 3 | ▶ | or ref $glob && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$glob->SUPER::isa($class) || ref $glob && do {
my $con = $glob->connection;
$con and $con->isa($class)
}
ref $glob and do {
my $con = $glob->connection;
$con and $con->isa($class)
}
MC/DC0%0/3
$glob->SUPER::isa($class) || ref $glob && do {
my $con = $glob->connection;
$con and $con->isa($class)
}
$glob->SUPER::isa($class)
ref $glob
do {
my $con = $glob->connection;
$con and $con->isa($class)
}
Truth table50%2/4
A || B and C
A= $glob->SUPER::isa($class)B= ref $globC= do {
my $con = $glob->connection;
$con and $con->isa($class)
}
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 2 | my $con = $glob->connection; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 2 | ▶ | $con && $con->isa($class); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | # Never delegate this. Connection::DESTROY clears its props. | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub DESTROY {} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 74 | 1; | |||||||||||||||||||||||||||||||||||||||||