File Coverage

blib/lib/Patro/N1.pm
Criterion Covered Total %
statement 40 54 74.0
branch 6 16 37.5
condition n/a
subroutine 16 21 76.1
pod n/a
total 62 91 68.1


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