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

BusyBird::SafeData - a wrapper of a complex data structure to access its internals safely

SYNOPSIS

use BusyBird::SafeData qw(safed);

my $data = {
    foo => {
        bar => [
            0, 1, 2
        ],
        buzz => { hoge => 100 }
    }
};

my $sd = safed($data);

$sd->original; ## => $data

$sd->val("foo", "bar", 1);              ## => 1
$sd->val("foo", "buzz", "FOO");         ## => undef
$sd->val("foo", "quux", "hoge");        ## => undef (and no autovivification)
$sd->val("foo", "bar", "FOO");          ## => undef (and no exception thrown)
$sd->val("foo", "buzz", "hoge", "FOO"); ## => undef (and no exception thrown)

$sd->array("foo", "bar");    ## => (0, 1, 2)
$sd->array("foo", "buzz");   ## => ()
$sd->array("foo", "bar", 1); ## => ()

DESCRIPTION

BusyBird::SafeData is a wrapper around a complex data structure to provide a safe way to access its internal data.

EXPORTABLE FUNCTIONS

The following function is exported only by request.

$sd = safed($data)

Same as BusyBird::SafeData->new($data).

CLASS METHODS

$sd = BusyBird::SafeData->new($data)

The constructor.

$data is any scalar. $sd wraps the given $data.

OBJECT METHODS

$data = $sd->original()

Returns the original $data that $sd wraps.

$val = $sd->val(@path)

Return the value specified by @path from $sd.

@path is a list of indices/keys from the root of the $sd down to the value you want. If it cannot traverse @path completely, it returns undef.

This method never autovivifies anything in $sd.

@vals = $sd->array(@path)

Return the list of values in the array-ref specified by @path. If it cannot traverse @path completely, it returns an empty list. If the value at the @path is not an array-ref, it returns an empty list.

AUTHOR

Toshio Ito <toshioito [at] cpan.org>