File Coverage

blib/lib/Patro/N1.pm
Criterion Covered Total %
statement 57 71 80.2
branch 8 18 44.4
condition 0 3 0.0
subroutine 21 28 75.0
pod n/a
total 86 120 71.6


line stmt bran cond sub pod time code
1             package Patro::N1;
2 9     9   57 use strict;
  9         20  
  9         363  
3 9     9   48 use warnings;
  9         28  
  9         418  
4              
5             # Patro::N1. Proxy class for HASH type references
6              
7             # we must keep this namespace very clean
8 9     9   44 use Carp ();
  9         16  
  9         306  
9              
10             use overload
11             '%{}' => sub {
12 9     9   41 no overloading;
  9         24  
  9         1689  
13 79 100   79   1452 if (${$_[0]}->{overloads}{'%{}'}) {
  79         331  
14 7         18 Patro::LeumJelly::deref_handler(@_,'%{}')
15             } else {
16 72         631 ${$_[0]}->{hash}
17 72         110 }
18             },
19             'nomethod' => \&Patro::LeumJelly::overload_handler,
20 1     1   5 '${}' => sub { Patro::LeumJelly::deref_handler(@_,'${}') },
21 3     3   16 '@{}' => sub { Patro::LeumJelly::deref_handler(@_,'@{}')},
22 9     9   57 ;
  9         18  
  9         205  
23              
24             # override UNIVERSAL methods
25             foreach my $umethod (keys %UNIVERSAL::) {
26 9     9   457 no strict 'refs';
  9         17  
  9         671  
27             *{$umethod} = sub {
28 6     6   896 my $proxy = shift;
        3      
        0      
        0      
29 6 100       21 if (!CORE::ref($proxy)) {
30             package
31             UNIVERSAL;
32 1         9 return &$umethod($proxy,@_);
33             }
34 9     9   44 no overloading;
  9         16  
  9         1602  
35 5 50       19 my $context = defined(wantarray) ? 1 + wantarray : 0;
36             return Patro::LeumJelly::proxy_request( $$proxy,
37 5         51 { id => $$proxy->{id}, topic => 'METHOD', command => $umethod,
38             has_args => @_ > 0, args => [ @_ ], context => $context }, @_ );
39             };
40             }
41              
42             sub AUTOLOAD {
43 14     14   4754 my $method = $Patro::N1::AUTOLOAD;
44 14         80 $method =~ s/.*:://;
45              
46 14         28 my $self = shift;
47 14         26 my $has_args = @_ > 0;
48 14         25 my $args = [ @_ ];
49              
50 14 100       37 my $context = defined(wantarray) ? 1 + wantarray : 0;
51              
52 9     9   56 no overloading;
  9         25  
  9         754  
53             return Patro::LeumJelly::proxy_request( $$self,
54             { id => $$self->{id},
55 14         119 topic => 'METHOD',
56             command => $method,
57             has_args => $has_args,
58             args => $args,
59             context => $context,
60             _autoload => 1 }, @_ );
61             }
62              
63             sub DESTROY {
64 0     0   0 my $self = shift;
65 9     9   46 no overloading;
  9         23  
  9         3788  
66 0 0       0 if ($$self->{_DESTROY}++) {
67 0         0 return;
68             }
69 0         0 my $socket = $$self->{socket};
70 0 0       0 if ($socket) {
71             my $response = Patro::LeumJelly::proxy_request(
72             $$self,
73             { id => $$self->{id},
74 0         0 topic => 'META',
75             command => 'destroy' } );
76 0 0 0     0 if (ref($response) && $response->{disconnect_ok}) {
77 0         0 close $socket;
78 0         0 delete $$self->{socket};
79             }
80             }
81             }
82              
83             ############################################################
84              
85             # tie class for hash proxy object. Operations on the proxy
86             # are forwarded to the remote server
87              
88             sub Patro::Tie::HASH::TIEHASH {
89 27     27   67 my ($pkg,$proxy) = @_;
90 27         157 return bless { obj => $proxy, id => $proxy->{id} }, $pkg;
91             }
92              
93             sub Patro::Tie::HASH::__ {
94 70     70   113 my $tied = shift;
95 70         107 my $name = shift;
96 70         105 my $context = shift;
97 70 50       180 if (!defined($context)) {
98 0 0       0 $context = defined(wantarray) ? 1 + wantarray : 0;
99             }
100             return Patro::LeumJelly::proxy_request(
101             $tied->{obj},
102             { topic => 'HASH',
103             command => $name,
104             context => $context,
105             has_args => @_ > 0,
106             args => [ @_ ],
107 70         688 id => $tied->{id} }, @_ );
108             }
109              
110 53     53   401 sub Patro::Tie::HASH::FETCH { return shift->__('FETCH',1,@_) }
111 7     7   34 sub Patro::Tie::HASH::STORE { return shift->__('STORE',0,@_) }
112 2     2   15 sub Patro::Tie::HASH::DELETE { return shift->__('DELETE',1,@_) }
113 0     0   0 sub Patro::Tie::HASH::CLEAR { return shift->__('CLEAR',0) }
114 8     8   34 sub Patro::Tie::HASH::EXISTS { return shift->__('EXISTS',1,@_) }
115 0     0     sub Patro::Tie::HASH::FIRSTKEY { return shift->__('FIRSTKEY',1,@_) }
116 0     0     sub Patro::Tie::HASH::NEXTKEY { return shift->__('NEXTKEY',1,@_) }
117 0     0     sub Patro::Tie::HASH::SCALAR { return shift->__('SCALAR',1) }
118              
119             1;