Promise-Me
view release on metacpan or search on metacpan
lib/Promise/Me.pm view on Meta::CPAN
{
$self->{data} = {};
}
return( $self );
}
sub DESTROY
{
my $self = shift( @_ );
my @info = caller();
print( STDERR __PACKAGE__, "::DESTROY: called from package '$info[0]' in file '$info[1]' at line $info[2]\n" ) if( $DEBUG );
};
sub FREEZE
{
my $self = shift( @_ );
my $serialiser = shift( @_ ) // '';
my $class = ref( $self ) || $self;
my %hash = %$self;
if( $self->{type} eq 'scalar' )
{
my $str = ${$self->{data}};
$hash{data} = \$str;
}
elsif( $self->{type} eq 'array' )
{
my @ref = @{$self->{data}};
$hash{data} = \@ref;
}
elsif( $self->{type} eq 'hash' )
{
my %ref = %{$self->{data}};
$hash{data} = \%ref;
}
return( [$class, \%hash] ) if( $serialiser eq 'Sereal' && Sereal::Encoder->VERSION <= version->parse( '4.023' ) );
return( $class, \%hash );
}
sub STORABLE_freeze { return( shift->FREEZE( @_ ) ); }
sub STORABLE_thaw { return( shift->THAW( @_ ) ); }
sub THAW
{
my( $self, undef, @args ) = @_;
my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
my $new;
# Storable pattern requires to modify the object it created rather than returning a new one
if( CORE::ref( $self ) )
{
foreach( CORE::keys( %$hash ) )
{
$self->{ $_ } = CORE::delete( $hash->{ $_ } );
}
$new = $self;
}
else
{
$new = CORE::bless( $hash => $class );
}
CORE::return( $new );
}
1;
# NOTE: POD
__END__
=encoding utf-8
=head1 NAME
Promise::Me - Fork Based Promise with Asynchronous Execution, Async, Await and Shared Data
=head1 SYNOPSIS
use Promise::Me; # exports async, await and share
my $p = Promise::Me->new(sub
{
# $_ is available as an array reference containing
# $_->[0] the code reference to the resolve method
# $_->[1] the code reference to the reject method
# Some regular code here
})->then(sub
{
my $res = shift( @_ ); # return value from the code executed above
# more processing...
})->then(sub
{
my $more = shift( @_ ); # return value from the previous then
# more processing...
})->catch(sub
{
my $exception = shift( @_ ); # error that occured is caught here
})->finally(sub
{
# final processing
})->then(sub
{
# A last then may be added after finally
};
# You can share data among processes for all systems, including Windows
my $data : shared = {};
my( $name, %attributes, @options );
share( $name, %attributes, @options );
my $p1 = Promise::Me->new( $code_ref )->then(sub
{
my $res = shift( @_ );
# more processing...
})->catch(sub
{
my $err = shift( @_ );
# Do something with the exception
});
my $p2 = Promise::Me->new( $code_ref )->then(sub
{
my $res = shift( @_ );
( run in 0.689 second using v1.01-cache-2.11-cpan-5735350b133 )