The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Venus::Try - Try Class

ABSTRACT

Try Class for Perl 5

SYNOPSIS

  package main;

  use Venus::Try;

  my $try = Venus::Try->new;

  $try->call(sub {
    my (@args) = @_;

    # try something

    return time;
  });

  $try->catch('Example::Error', sub {
    my ($caught) = @_;

    # caught an error (exception)

    return;
  });

  $try->default(sub {
    my ($caught) = @_;

    # catch the uncaught

    return;
  });

  $try->finally(sub {
    my (@args) = @_;

    # always run after try/catch

    return;
  });

  my @args;

  my $result = $try->result(@args);

DESCRIPTION

This package provides an object-oriented interface for performing complex try/catch operations.

ATTRIBUTES

This package has the following attributes:

invocant

  invocant(Object)

This attribute is read-only, accepts (Object) values, and is optional.

arguments

  arguments(ArrayRef)

This attribute is read-only, accepts (ArrayRef) values, and is optional.

on_try

  on_try(CodeRef)

This attribute is read-write, accepts (CodeRef) values, and is optional.

on_catch

  on_catch(ArrayRef[CodeRef])

This attribute is read-write, accepts (ArrayRef[CodeRef]) values, is optional, and defaults to [].

on_default

  on_default(CodeRef)

This attribute is read-write, accepts (CodeRef) values, and is optional.

on_finally

  on_finally(CodeRef)

This attribute is read-write, accepts (CodeRef) values, and is optional.

INHERITS

This package inherits behaviors from:

Venus::Kind::Utility

METHODS

This package provides the following methods:

call

  call(Str | CodeRef $method) (Try)

The call method takes a method name or coderef, registers it as the tryable routine, and returns the object. When invoked, the callback will received an invocant if one was provided to the constructor, the default arguments if any were provided to the constructor, and whatever arguments were provided by the invocant.

Since 0.01

call example 1
  package main;

  use Venus::Try;

  my $try = Venus::Try->new;

  my $call = $try->call(sub {
    my (@args) = @_;

    return [@args];
  });

  # bless({ on_catch => ... }, "Venus::Try")

callback

  callback(Str | CodeRef $method) (CodeRef)

The callback method takes a method name or coderef, and returns a coderef for registration. If a coderef is provided this method is mostly a passthrough.

Since 0.01

callback example 1
  package main;

  use Venus::Try;

  my $try = Venus::Try->new;

  my $callback = $try->callback(sub {
    my (@args) = @_;

    return [@args];
  });

  # sub { ... }
callback example 2
  package Example1;

  sub new {
    bless {};
  }

  sub test {
    my (@args) = @_;

    return [@args];
  }

  package main;

  use Venus::Try;

  my $try = Venus::Try->new(
    invocant => Example1->new,
  );

  my $callback = $try->callback('test');

  # sub { ... }
callback example 3
  package main;

  use Venus::Try;

  my $try = Venus::Try->new;

  my $callback = $try->callback('missing_method');

  # Exception! Venus::Try::Error (isa Venus::Error)

catch

  catch(Str $isa, Str | CodeRef $method) (Try)

The catch method takes a package or ref name, and when triggered checks whether the captured exception is of the type specified and if so executes the given callback.

Since 0.01

catch example 1
  package main;

  use Venus::Try;

  my $try = Venus::Try->new;

  $try->call(sub {
    my (@args) = @_;

    die $try;
  });

  my $catch = $try->catch('Venus::Try', sub {
    my (@args) = @_;

    return [@args];
  });

  # bless({ on_catch => ... }, "Venus::Try")

default

  default(Str | CodeRef $method) (Try)

The default method takes a method name or coderef and is triggered if no catch conditions match the exception thrown.

Since 0.01

default example 1
  package main;

  use Venus::Try;

  my $try = Venus::Try->new;

  $try->call(sub {
    my (@args) = @_;

    die $try;
  });

  my $default = $try->default(sub {
    my (@args) = @_;

    return [@args];
  });

  # bless({ on_catch => ... }, "Venus::Try")

error

  error(Ref $variable) (Try)

The error method takes a scalar reference and assigns any uncaught exceptions to it during execution.

Since 0.01

error example 1
  package main;

  use Venus::Try;

  my $try = Venus::Try->new;

  $try->call(sub {
    my (@args) = @_;

    die $try;
  });

  my $error = $try->error(\my $object);

  # bless({ on_catch => ... }, "Venus::Try")

execute

  execute(CodeRef $code, Any @args) (Any)

The execute method takes a coderef and executes it with any given arguments. When invoked, the callback will received an invocant if one was provided to the constructor, the default arguments if any were provided to the constructor, and whatever arguments were passed directly to this method. This method can return a list of values in list-context.

Since 0.01

execute example 1
  package Example2;

  sub new {
    bless {};
  }

  package main;

  use Venus::Try;

  my $try = Venus::Try->new(
    invocant => Example2->new,
    arguments => [1,2,3],
  );

  my $execute = $try->execute(sub {
    my (@args) = @_;

    return [@args];
  });

  # [bless({}, "Example2"), 1, 2, 3]

finally

  finally(Str | CodeRef $method) (Try)

The finally method takes a package or ref name and always executes the callback after a try/catch operation. The return value is ignored. When invoked, the callback will received an invocant if one was provided to the constructor, the default arguments if any were provided to the constructor, and whatever arguments were provided by the invocant.

Since 0.01

finally example 1
  package Example3;

  sub new {
    bless {};
  }

  package main;

  use Venus::Try;

  my $try = Venus::Try->new(
    invocant => Example3->new,
    arguments => [1,2,3],
  );

  $try->call(sub {
    my (@args) = @_;

    return $try;
  });

  my $finally = $try->finally(sub {
    my (@args) = @_;

    $try->{args} = [@args];
  });

  # bless({ on_catch => ... }, "Venus::Try")

maybe

  maybe() (Try)

The maybe method registers a default catch condition that returns falsy, i.e. an empty string, if an exception is encountered.

Since 0.01

maybe example 1
  package main;

  use Venus::Try;

  my $try = Venus::Try->new;

  $try->call(sub {
    my (@args) = @_;

    die $try;
  });

  my $maybe = $try->maybe;

  # bless({ on_catch => ... }, "Venus::Try")

no_catch

  no_catch() (Try)

The no_catch method removes any configured catch conditions and returns the object.

Since 0.01

no_catch example 1
  package main;

  use Venus::Try;

  my $try = Venus::Try->new;

  $try->call(sub {
    my (@args) = @_;

    die $try;
  });

  $try->catch('Venus::Try', sub {
    my (@args) = @_;

    return [@args];
  });


  my $no_catch = $try->no_catch;

  # bless({ on_catch => ... }, "Venus::Try")

no_default

  no_default() (Try)

The no_default method removes any configured default condition and returns the object.

Since 0.01

no_default example 1
  package main;

  use Venus::Try;

  my $try = Venus::Try->new;

  $try->call(sub {
    my (@args) = @_;

    die $try;
  });

  my $default = $try->default(sub {
    my (@args) = @_;

    return [@args];
  });

  my $no_default = $try->no_default;

  # bless({ on_catch => ... }, "Venus::Try")

no_finally

  no_finally() (Try)

The no_finally method removes any configured finally condition and returns the object.

Since 0.01

no_finally example 1
  package Example4;

  sub new {
    bless {};
  }

  package main;

  use Venus::Try;

  my $try = Venus::Try->new(
    invocant => Example4->new,
    arguments => [1,2,3],
  );

  $try->call(sub {
    my (@args) = @_;

    return $try;
  });

  $try->finally(sub {
    my (@args) = @_;

    $try->{args} = [@args];
  });

  my $no_finally = $try->no_finally;

  # bless({ on_catch => ... }, "Venus::Try")

no_try

  no_try() (Try)

The no_try method removes any configured try operation and returns the object.

Since 0.01

no_try example 1
  package main;

  use Venus::Try;

  my $try = Venus::Try->new;

  $try->call(sub {
    my (@args) = @_;

    return [@args];
  });

  my $no_try = $try->no_try;

  # bless({ on_catch => ... }, "Venus::Try")

result

  result(Any @args) (Any)

The result method executes the try/catch/default/finally logic and returns either 1) the return value from the successfully tried operation 2) the return value from the successfully matched catch condition if an exception was thrown 3) the return value from the default catch condition if an exception was thrown and no catch condition matched. When invoked, the try and finally callbacks will received an invocant if one was provided to the constructor, the default arguments if any were provided to the constructor, and whatever arguments were passed directly to this method. This method can return a list of values in list-context.

Since 0.01

result example 1
  package main;

  use Venus::Try;

  my $try = Venus::Try->new;

  $try->call(sub {
    my (@args) = @_;

    return [@args];
  });

  my $result = $try->result;

  # []
result example 2
  package main;

  use Venus::Try;

  my $try = Venus::Try->new;

  $try->call(sub {
    my (@args) = @_;

    return [@args];
  });

  my $result = $try->result(1..5);

  # [1..5]
result example 3
  package main;

  use Venus::Try;

  my $try = Venus::Try->new;

  $try->call(sub {die});

  my $result = $try->result;

  # Exception! Venus::Error