File Coverage

blib/lib/Patro/N1.pm
Criterion Covered Total %
statement 40 54 74.0
branch 6 16 37.5
condition n/a
subroutine 15 21 71.4
pod n/a
total 61 91 67.0


line stmt bran cond sub pod time code
1             package Patro::N1;
2 8     8   77 use strict;
  8         16  
  8         313  
3 8     8   44 use warnings;
  8         16  
  8         326  
4              
5             # Patro::N1. Proxy class for HASH type references
6              
7             # we must keep this namespace very clean
8 8     8   39 use Carp ();
  8         17  
  8         597  
9              
10             use overload
11 63     63   936 '%{}' => sub { ${$_[0]}->{hash} },
  63         424  
12 8         173 'nomethod' => \&Patro::LeumJelly::overload_handler,
13 8     8   46 ;
  8         13  
14              
15             # override UNIVERSAL methods
16             foreach my $umethod (keys %UNIVERSAL::) {
17 8     8   461 no strict 'refs';
  8         22  
  8         4925  
18             *{$umethod} = sub {
19 6     6   11 my $proxy = shift;
        3      
        0      
20 6 100       17 if (!CORE::ref($proxy)) {
21             package UNIVERSAL;
22 1         8 return &$umethod($proxy,@_);
23             }
24 5 50       15 my $context = defined(wantarray) ? 1 + wantarray : 0;
25             return Patro::LeumJelly::proxy_request( $$proxy,
26 5         55 { id => $$proxy->{id}, topic => 'METHOD', command => $umethod,
27             has_args => @_ > 0, args => [ @_ ], context => $context }, @_ );
28             };
29             }
30              
31             sub AUTOLOAD {
32 12     12   4053 my $method = $Patro::N1::AUTOLOAD;
33 12         70 $method =~ s/.*:://;
34              
35 12         22 my $self = shift;
36 12         20 my $has_args = @_ > 0;
37 12         26 my $args = [ @_ ];
38              
39 12 100       73 my $context = defined(wantarray) ? 1 + wantarray : 0;
40              
41             return Patro::LeumJelly::proxy_request( $$self,
42             { id => $$self->{id},
43 12         122 topic => 'METHOD',
44             command => $method,
45             has_args => $has_args,
46             args => $args,
47             context => $context,
48             _autoload => 1 }, @_ );
49             }
50              
51             sub DESTROY {
52 0     0   0 my $self = shift;
53 0 0       0 if ($$self->{_DESTROY}++) {
54 0         0 return;
55             }
56 0         0 my $socket = $$self->{socket};
57 0 0       0 if ($socket) {
58              
59             # XXX - shouldn't disconnect on every object destruction,
60             # only when all of the wrapped objects associated with a
61             # client have been destroyed, or during global
62             # destruction
63              
64             my $response = Patro::LeumJelly::proxy_request(
65             $$self,
66             { id => $$self->{id},
67 0         0 topic => 'META',
68             #command => 'disconnect' } );
69             command => 'destroy' } );
70 0 0       0 if ($response->{disconnect_ok}) {
71 0         0 close $socket;
72 0         0 delete $$self->{socket};
73             }
74             }
75             }
76              
77             ############################################################
78              
79             # tie class for hash proxy object. Operations on the proxy
80             # are forwarded to the remote server
81              
82             sub Patro::Tie::HASH::TIEHASH {
83 22     22   52 my ($pkg,$proxy) = @_;
84 22         108 return bless { obj => $proxy, id => $proxy->{id} }, $pkg;
85             }
86              
87             sub Patro::Tie::HASH::__ {
88 61     61   84 my $tied = shift;
89 61         77 my $name = shift;
90 61         69 my $context = shift;
91 61 50       129 if (!defined($context)) {
92 0 0       0 $context = defined(wantarray) ? 1 + wantarray : 0;
93             }
94             return Patro::LeumJelly::proxy_request(
95             $tied->{obj},
96             { topic => 'HASH',
97             command => $name,
98             context => $context,
99             has_args => @_ > 0,
100             args => [ @_ ],
101 61         457 id => $tied->{id} }, @_ );
102             }
103              
104 45     45   324 sub Patro::Tie::HASH::FETCH { return shift->__('FETCH',1,@_) }
105 6     6   22 sub Patro::Tie::HASH::STORE { return shift->__('STORE',0,@_) }
106 2     2   9 sub Patro::Tie::HASH::DELETE { return shift->__('DELETE',1,@_) }
107 0     0   0 sub Patro::Tie::HASH::CLEAR { return shift->__('CLEAR',0) }
108 8     8   23 sub Patro::Tie::HASH::EXISTS { return shift->__('EXISTS',1,@_) }
109 0     0     sub Patro::Tie::HASH::FIRSTKEY { return shift->__('FIRSTKEY',1,@_) }
110 0     0     sub Patro::Tie::HASH::NEXTKEY { return shift->__('NEXTKEY',1,@_) }
111 0     0     sub Patro::Tie::HASH::SCALAR { return shift->__('SCALAR',1) }
112              
113             1;