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