Process::YAML - The Process::Serializable role implemented by YAML


Process-YAML documentation Contained in the Process-YAML distribution.

Index


Code Index:

NAME

Top

Process::YAML - The Process::Serializable role implemented by YAML

SYNOPSIS

Top

  package MyYAMLProcess;

  use base 'Process::YAML',
           'Process';

  sub prepare {
      ...
  }

  sub run {
      ...
  }

  1;

DESCRIPTION

Top

Process::YAML provides an implementation of the Process::Serializable role using the YAML::Syck module from CPAN. It is not itself a subclass of Process so you will need to inherit from both.

Objects that inherit from Process::YAML must follow the new, prepare, run rules of Process::Serializable.

YAML::Syck was chosen over YAML because YAML::Syck is much faster. Furthermore, YAML uses Spiffy which I could not get to play well with the inheritance scheme of the Process framework at the time (Spiffy 0.26). By now, Brian Ingerson has released a fixed version of Spiffy (0.27), so YAML 0.52 and higher is compatible with Process::YAML.

METHODS

Using this class as an additional base class for your Process based classes will add two methods to your class as defined by the Process::Serializable documentation. Please refer to that module for a description of the interface.

serialize
deserialize

SUPPORT

Top

Bugs should be reported via the CPAN bug tracker at

http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Process-YAML

For other issues, contact the author.

AUTHOR

Top

Steffen Mueller <modules at steffen-mueller dot net>, http://steffen-mueller.net/

COPYRIGHT

Top


Process-YAML documentation Contained in the Process-YAML distribution.

package Process::YAML;

# Process that is compatible with Storable after new, and after run.

use 5.005;
use strict;
use base 'Process::Serializable';
use Fcntl qw/:flock/;
use YAML::Syck   ();
use IO::Handle   ();
use IO::File     ();
use IO::String   ();
use Scalar::Util ();
use Params::Util qw/_INSTANCE/;

use vars qw{$VERSION};
BEGIN {
	$VERSION = '0.04';
}

BEGIN {
	unless ( IO::String->isa('IO::Handle') ) {
		push @IO::String::ISA, 'IO::Handle';
	}
	unless ( IO::String->isa('IO::Seekable') ) {
		push @IO::String::ISA, 'IO::Seekable';
	}
}

sub serialize {
	my $self = shift;

	# Serialize to a string (via a handle)
	if ( Params::Util::_SCALAR0($_[0]) ) {
		my $handle = IO::String->new($_[0]);
		return print $handle YAML::Syck::Dump($self);
	}

	# Serialize to a generic handle
	if ( Params::Util::_INSTANCE($_[0], 'IO::Handle') or fileno($_[0]) ) {
		my $handle = $_[0];
		return print $handle YAML::Syck::Dump($self);
	}

	# Serialize to a file name (locking it)
	if ( defined $_[0] and ! ref $_[0] and length $_[0] ) {
		my $fh;
		my $mode = '+<';
		if ( not -e $_[0] ) {
			$mode = '+>';
		}
		if ( not open($fh, $mode, $_[0]) ) {
			return undef;
		}
		if ( not flock($fh, LOCK_EX) ) {
			return undef;
		}
		if ( not truncate($fh, 0) ) {
			return undef;
		}
		if ( not print $fh YAML::Syck::Dump($self) ) {
			return undef;
		}
		if ( not close $fh ) {
			return undef;
		}
		return 1;
	}

	# We don't support anything else
	undef;
}

sub deserialize {
	my $class = shift;

	# Deserialize from a string
	if ( Params::Util::_SCALAR0($_[0]) ) {
		return YAML::Syck::Load(${$_[0]});
	}

	# Deserialize from a generic handle
	if ( Params::Util::_INSTANCE($_[0], 'IO::Handle') or fileno($_[0]) ) {
		my $handle = $_[0];
		return YAML::Syck::Load(join '', <$handle>);
	}

	# Deserialize from a file name (locking it)
	if ( defined $_[0] and ! ref $_[0] and length $_[0] ) {
		my $fh;
		if ( not open($fh, '<', $_[0]) ) {
			return undef;
		}
		if ( not flock($fh, LOCK_SH) ) {
			return undef;
		}
		return YAML::Syck::Load(join '', <$fh>);
	}

	# We don't support anything else
	undef;
}

1;

__END__