File Coverage

blib/lib/Process/YAML.pm
Criterion Covered Total %
statement 68 77 88.3
branch 20 32 62.5
condition 10 18 55.5
subroutine 15 15 100.0
pod 2 2 100.0
total 115 144 79.8


line stmt bran cond sub pod time code
1             package Process::YAML;
2              
3             # Process that is compatible with Storable after new, and after run.
4              
5 2     2   43201 use 5.005;
  2         9  
  2         85  
6 2     2   11 use strict;
  2         4  
  2         71  
7 2     2   9 use base 'Process::Serializable';
  2         4  
  2         1946  
8 2     2   3008 use Fcntl qw/:flock/;
  2         4  
  2         424  
9 2     2   2125 use YAML::Syck ();
  2         4850  
  2         69  
10 2     2   2112 use IO::Handle ();
  2         16706  
  2         51  
11 2     2   2133 use IO::File ();
  2         5062  
  2         59  
12 2     2   1609 use IO::String ();
  2         6290  
  2         54  
13 2     2   18 use Scalar::Util ();
  2         5  
  2         48  
14 2     2   2248 use Params::Util qw/_INSTANCE/;
  2         6069  
  2         169  
15              
16 2     2   13 use vars qw{$VERSION};
  2         4  
  2         77  
17             BEGIN {
18 2     2   122 $VERSION = '0.04';
19             }
20              
21             BEGIN {
22 2 50   2   41 unless ( IO::String->isa('IO::Handle') ) {
23 2         22 push @IO::String::ISA, 'IO::Handle';
24             }
25 2 50       21 unless ( IO::String->isa('IO::Seekable') ) {
26 2         903 push @IO::String::ISA, 'IO::Seekable';
27             }
28             }
29              
30             sub serialize {
31 4     4 1 10211 my $self = shift;
32              
33             # Serialize to a string (via a handle)
34 4 100       28 if ( Params::Util::_SCALAR0($_[0]) ) {
35 1         12 my $handle = IO::String->new($_[0]);
36 1         73 return print $handle YAML::Syck::Dump($self);
37             }
38              
39             # Serialize to a generic handle
40 3 100 100     37 if ( Params::Util::_INSTANCE($_[0], 'IO::Handle') or fileno($_[0]) ) {
41 2         4 my $handle = $_[0];
42 2         7 return print $handle YAML::Syck::Dump($self);
43             }
44              
45             # Serialize to a file name (locking it)
46 1 50 33     14 if ( defined $_[0] and ! ref $_[0] and length $_[0] ) {
      33        
47 1         3 my $fh;
48 1         3 my $mode = '+<';
49 1 50       24 if ( not -e $_[0] ) {
50 1         4 $mode = '+>';
51             }
52 1 50       132 if ( not open($fh, $mode, $_[0]) ) {
53 0         0 return undef;
54             }
55 1 50       12 if ( not flock($fh, LOCK_EX) ) {
56 0         0 return undef;
57             }
58 1 50       48 if ( not truncate($fh, 0) ) {
59 0         0 return undef;
60             }
61 1 50       5 if ( not print $fh YAML::Syck::Dump($self) ) {
62 0         0 return undef;
63             }
64 1 50       198 if ( not close $fh ) {
65 0         0 return undef;
66             }
67 1         9 return 1;
68             }
69              
70             # We don't support anything else
71 0         0 undef;
72             }
73              
74             sub deserialize {
75 4     4 1 3185 my $class = shift;
76              
77             # Deserialize from a string
78 4 100       17 if ( Params::Util::_SCALAR0($_[0]) ) {
79 1         3 return YAML::Syck::Load(${$_[0]});
  1         7  
80             }
81              
82             # Deserialize from a generic handle
83 3 100 100     30 if ( Params::Util::_INSTANCE($_[0], 'IO::Handle') or fileno($_[0]) ) {
84 2         3 my $handle = $_[0];
85 2         34 return YAML::Syck::Load(join '', <$handle>);
86             }
87              
88             # Deserialize from a file name (locking it)
89 1 50 33     16 if ( defined $_[0] and ! ref $_[0] and length $_[0] ) {
      33        
90 1         3 my $fh;
91 1 50       43 if ( not open($fh, '<', $_[0]) ) {
92 0         0 return undef;
93             }
94 1 50       12 if ( not flock($fh, LOCK_SH) ) {
95 0         0 return undef;
96             }
97 1         34 return YAML::Syck::Load(join '', <$fh>);
98             }
99              
100             # We don't support anything else
101 0           undef;
102             }
103              
104             1;
105              
106             __END__