The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Scope::Container - scope based container

SYNOPSIS

use Scope::Container;

sub getdb {
    if ( my $dbh = scope_container('db') ) {
        return $dbh;
    } else {
        my $dbh = DBI->connect(...);
        scope_container('db', $dbh)
        return $dbh;
    }
}

for (1..3) {
  my $contaier = start_scope_container();
  getdb(); # do connect
  getdb(); # from container
  getdb(); # from container
  # $container scope out and disconnect from db
}

getdb(); # do connect

DESCRIPTION

Scope::Container is scope based container for temporary items and Database Connections.

EXPORTED FUNCTION

my $scope_container = start_scope_container([-clear => 1]);

Initializing container. The default behavior is inherited all the previous container's data. If set -clear arguments, save previous container's data and create new data.

return values is Scope::Container object. if this object scope exits, current container will be removed, return to the previous state.

my $value = scope_container($key:Str[,$val:Any]);

getter, setter of container data.

in_scope_container

Check if context is initialized

LIMITATION

There is a limit to the order in which the Scope::Container object is deleted. If race condition found, remove all data.

my $sc = start_scope_container();
scope_container('bar', 'foo');
my $sc2 = start_scope_container();
scope_container('bar', 'baz');

undef $sc;
scope_container('bar'); #null

AUTHOR

Masahiro Nagano <kazeburo {at} gmail.com>

Fuji, Goro (gfx)

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.