File Coverage

blib/lib/Process/Storable.pm
Criterion Covered Total %
statement 67 69 97.1
branch 20 28 71.4
condition 8 12 66.6
subroutine 13 13 100.0
pod 2 2 100.0
total 110 124 88.7


line stmt bran cond sub pod time code
1             package Process::Storable;
2              
3 5     5   5527 use 5.00503;
  5         18  
  5         167  
4 5     5   24 use strict;
  5         6  
  5         136  
5 5     5   3178 use Storable ();
  5         11448  
  5         93  
6 5     5   3186 use IO::Handle ();
  5         23597  
  5         122  
7 5     5   4337 use IO::String ();
  5         12477  
  5         92  
8 5     5   32 use Scalar::Util ();
  5         19  
  5         70  
9 5     5   2067 use Params::Util ();
  5         9501  
  5         87  
10 5     5   1337 use Process::Serializable ();
  5         10  
  5         91  
11              
12 5     5   23 use vars qw{$VERSION @ISA};
  5         10  
  5         421  
13             BEGIN {
14 5     5   25 $VERSION = '0.30';
15 5         87 @ISA = 'Process::Serializable';
16              
17             # Hack IO::String to be a real IO::Handle
18 5 50       96 unless ( IO::String->isa('IO::Handle') ) {
19 5         2385 @IO::String::ISA = qw{IO::Handle IO::Seekable};
20             }
21             }
22              
23             sub serialize {
24 8     8 1 171969 my $self = shift;
25              
26             # Serialize to a named file (locking it)
27 8 100 66     113 if ( defined $_[0] and ! ref $_[0] and length $_[0] ) {
      66        
28 1         5 return Storable::lock_nstore($self, shift);
29             }
30              
31             # Serialize to a string (via a handle)
32 7 100       57 if ( Params::Util::_SCALAR0($_[0]) ) {
33 1         2 my $string = shift;
34 1         6 $$string = 'pst0' . Storable::nfreeze($self);
35 1         92 return 1;
36             }
37              
38             # Serialize to a generic handle
39 6 100       32 if ( defined fileno($_[0]) ) {
40 5         28 local $/ = undef;
41 5         53 return Storable::nstore_fd($self, shift);
42             }
43              
44             # Serialize to an IO::Handle object
45 1 50       13 if ( Params::Util::_INSTANCE($_[0], 'IO::Handle') ) {
46 1         5 my $string = Storable::nfreeze($self);
47 1         43 my $iohandle = shift;
48 1 50       5 $iohandle->print( 'pst0' ) or return undef;
49 1 50       29 $iohandle->print( $string ) or return undef;
50 1         17 return 1;
51             }
52              
53             # We don't support anything else
54 0         0 undef;
55             }
56              
57             sub deserialize {
58 7     7 1 74150 my $class = shift;
59 7         64 my $self = $class->_deserialize(@_);
60              
61             # Integrity check
62 7 50       430 Params::Util::_INSTANCE($self, $class) or return undef;
63              
64 7         24 $self;
65             }
66              
67             sub _deserialize {
68 7     7   14 my $class = shift;
69              
70             # Serialize from a named file (locking it)
71 7 100 66     88 if ( defined $_[0] and ! ref $_[0] and length $_[0] ) {
      66        
72 1         3 return Storable::lock_retrieve(shift);
73             }
74              
75             # Serialize from a string (via a handle)
76 6 100       37 if ( Params::Util::_SCALAR0($_[0]) ) {
77 1         2 my $string = shift;
78              
79             # Remove the magic header if it exists
80 1 50       6 if ( substr($$string, 0, 4) eq 'pst0' ) {
81 1         3 substr($$string, 0, 4, '');
82             }
83              
84 1         5 return Storable::thaw($$string);
85             }
86              
87             # Serialize from a generic handle
88 5 100       34 if ( defined fileno($_[0]) ) {
89 4         49 return Storable::retrieve_fd(shift);
90             }
91              
92             # Serialize from an IO::Handle object
93 1 50       10 if ( Params::Util::_INSTANCE($_[0], 'IO::Handle') ) {
94 1         4 local $/ = undef;
95 1         6 my $string = $_[0]->getline;
96              
97             # Remove the magic header if it exists
98 1 50       14 if ( substr($string, 0, 4) eq 'pst0' ) {
99 1         2 substr($string, 0, 4, '');
100             }
101              
102 1         4 return Storable::thaw($string);
103             }
104              
105             # We don't support anything else
106 0           undef;
107             }
108              
109             1;
110              
111             __END__