File Coverage

blib/lib/Patro/N2.pm
Criterion Covered Total %
statement 53 63 84.1
branch 6 16 37.5
condition n/a
subroutine 17 20 85.0
pod n/a
total 76 99 76.7


line stmt bran cond sub pod time code
1             package Patro::N2;
2 4     4   27 use strict;
  4         10  
  4         136  
3 4     4   17 use warnings;
  4         6  
  4         197  
4              
5             # Patro::N2. Proxy class for SCALAR type references
6              
7             # we must keep this namespace very clean
8 4     4   21 use Carp ();
  4         13  
  4         124  
9              
10             use overload
11             '${}' => sub {
12 4     4   16 no overloading '%{}', '${}';
  4         6  
  4         696  
13 17 50   17   1338 if ($_[0]->{overloads}{'${}'}) {
14 0         0 Patro::LeumJelly::deref_handler(@_,'${}');
15             } else {
16             $_[0]->{scalar}
17 17         93 }
18             },
19             'nomethod' => \&Patro::LeumJelly::overload_handler,
20 0     0   0 '@{}' => sub { Patro::LeumJelly::deref_handler(@_,'@{}') },
21 0     0   0 '%{}' => sub { Patro::LeumJelly::deref_handler(@_,'%{}') },
22 4     4   21 ;
  4         7  
  4         75  
23              
24             # override UNIVERSAL methods
25             foreach my $umethod (keys %UNIVERSAL::) {
26 4     4   180 no strict 'refs';
  4         9  
  4         339  
27             *{$umethod} = sub {
28 4     4   728 my $proxy = shift;
29 4 100       13 if (!CORE::ref($proxy)) {
30             package
31             UNIVERSAL;
32 1         15 return &$umethod($proxy,@_);
33             }
34 3 50       8 my $context = defined(wantarray) ? 1 + wantarray : 0;
35 4     4   21 no overloading '%{}';
  4         5  
  4         653  
36             return Patro::LeumJelly::proxy_request( $proxy,
37 3         35 { id => $proxy->{id}, topic => 'METHOD', command => $umethod,
38             has_args => @_ > 0, args => [ @_ ], context => $context }, @_ );
39             };
40             }
41              
42             sub AUTOLOAD {
43 2     2   391 my $method = $Patro::N2::AUTOLOAD;
44 2         15 $method =~ s/.*:://;
45              
46 2         7 my $self = shift;
47 2         5 my $has_args = @_ > 0;
48 2         7 my $args = [ @_ ];
49              
50 2 50       6 my $context = defined(wantarray) ? 1 + wantarray : 0;
51 4     4   22 no overloading '%{}';
  4         7  
  4         314  
52              
53             return Patro::LeumJelly::proxy_request( $self,
54             { id => $self->{id},
55 2         20 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 4     4   22 no overloading '%{}';
  4         11  
  4         452  
66 0 0       0 return if $self->{_DESTROY}++;
67 0         0 my $socket = $self->{socket};
68 0 0       0 if ($socket) {
69              
70             # XXX - shouldn't disconnect on every object destruction,
71             # only when all of the wrapped objects associated with a
72             # client have been destroyed, or during global
73             # destruction
74              
75             Patro::LeumJelly::proxy_request( $self,
76             { id => $self->{id},
77 0         0 topic => 'META',
78             command => 'disconnect' } );
79 0         0 close $socket;
80             }
81             }
82              
83             # tie class for proxy object. Operations on the proxy object
84             # are forwarded to the remote server
85              
86             sub Patro::Tie::SCALAR::TIESCALAR {
87 5     5   26 my ($pkg,$proxy) = @_;
88 4     4   21 no overloading '%{}';
  4         5  
  4         729  
89 5         27 return bless { obj => $proxy, id => $proxy->{id} }, $pkg;
90             }
91              
92             sub Patro::Tie::SCALAR::__ {
93 20     20   29 my $tied = shift;
94 20         26 my $name = shift;
95 20         25 my $context = shift;
96 20 50       41 if (!defined($context)) {
97 0 0       0 $context = defined(wantarray) ? 1 + wantarray : 0;
98             }
99             return Patro::LeumJelly::proxy_request( $tied->{obj},
100             { topic => 'SCALAR',
101             command => $name,
102             context => $context,
103             has_args => @_ > 0,
104             args => [ @_ ],
105 20         185 id => $tied->{id} }, @_ );
106             }
107              
108 14     14   198 sub Patro::Tie::SCALAR::FETCH { return shift->__('FETCH',1) }
109 6     6   19 sub Patro::Tie::SCALAR::STORE { return shift->__('STORE',0,@_) }
110              
111             1;