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

Anonymous::Object - Generate Anonymous Objects

VERSION

Version 0.07

SYNOPSIS

Quick summary of what the module does.

        use Anonymous::Object;

        my $anon = Anonymous::Object->new({
                object_name => 'Custom::Object'
        })->hash_to_object({
                a => 1,
                b => 2,
                c => 3
        }, autotype => 1, set => 1);

        $anon->a; # 1
        $anon->b; # 2
        $anon->c; # 3

        ....

        my $anon = Anonymous::Object->new({});

        $anon->add_method({
                name => 'test_accessor',
                clearer => 1,
                predicate => 1,
                get => 1,
                set => 1,
                ref => 1,
                reftype => 1,
                type => 'Str',
                default => 'xyz',
        });

        $anon->build;

DESCRIPTION

Anonymous object. Anonymous simply means nameless. An object which has no reference is known as an anonymous object. It can be used at the time of object creation only. If you have to use an object only once, an anonymous object is a good approach.

SUBROUTINES/METHODS

new

Instantiate a new Anonymous::Object object.

        my $anon = Anonymous::Object->new({
                object_name => 'Custom::Object',
                types => {
                        Str => 1,
                        HashRef => 1,
                        ...
                },
                type_library => 'Types::Standard',
                type_map => {
                        HASH => 'HashRef',
                        ARRAY => 'ArrayRef',
                        STRING => 'Str',
                        SCALAR => 'ScalarRef',
                        REF => 'Ref',
                        CODE => 'CodeRef',
                        GLOB => 'GlobRef',
                        NUM => 'Num',
                        INT => 'Int',
                        default => 'Any'
                },
                meta => {
                        sub1 => 'return $_[1] * $_[2]',
                        sub2 => 'return $_[0]->{sub2};'
                },
                default => {
                        sub2 => 'xyz'
                }
        });

object_name

The object name used when bulding the Anonymous::Object. Expects a Str.

types

The types that will be loaded into the Anonymous::Object when built. Expects a HashRef.

type_library

The type library that you would like the Anonymous::Object to use. The default is Types::Standard. Expects a Str.

type_map

The mapping that is used when auto detecting types from perl data structures. Expects a HashRef.

meta

The method meta store. Expects a HashRef.

default

The default values for Anonymous::Object accessors. Expects a HashRef.

clean

Instantiate a clean Anonymous::Object passing through object_name, type_library and type_map;

        my $clean_anon = $anon->clean;

hash_to_object

Convert a perl hash into a single level perl object. Expects param $hash to be a HashRef.

        my $hash = {
                one => 1,
                two => 2,
                three => {
                        four => 4,
                        five => [
                                {
                                        six => 6
                                }
                        ],
                }
        };

        my $obj = $obj->hash_to_object($hash, %method_options)

        $hash->one; # 1
        $hash->three->{four}; # 4
        $hash->three->{five}->[0]->{six}; # 6

hash_to_object_context

Convert a perl hash that contain values which are sub routines where you need to keep context.

        my $num = 1;
        my $hash = {
                add => { $num += $_[0] },
                minus => { $num -= $_[0] },
        };

        my $obj = $obj->hash_to_object_context($hash, %method_options)

        $hash->add(1); # 2
        $hash->minus(1); # 1

hash_to_nested_object

Convert a perl hash into a multi level perl object. Expects param $hash to be a HashRef.

        my $hash = {
                one => 1,
                two => 2,
                three => {
                        four => 4,
                        five => [
                                {
                                        six => 6
                                }
                        ],
                }
        };

        my $obj = $obj->hash_to_nested_object($hash, %method_options)

        $hash->one; # 1
        $hash->three->four; # 4
        $hash->three->five->[0]->six; # 6

array_to_nested_object

Convert a perl array into a multi level perl object. Expects param $array to be a ArrayRef.

        my $array = [{
                one => 1,
                two => 2,
                three => {
                        four => 4,
                        five => [
                                {
                                        six => 6
                                }
                        ],
                }
        }];

        my $obj = $obj->array_to_nested_object($hash, %method_options)

        $array->[0]->one; # 1
        $array->[0]->three->four; # 4
        $array->[0]->three->five->[0]->six; # 6

add_new

Builds the 'new' method for the Anonymous::Object. Expects param $new to be a HashRef of default values.

        $obj->add_new({
                one => 1,
                two => 2,
                three => {
                        four => 4,
                        five => [
                                {
                                        six => 6
                                }
                        ],
                }
        });

add_methods

Add multiple methods to the Anonymous::Object. Expects param $methods to be a ArrayRef of HashRefs that represent a method..

        my $anon = Anonymous::Object->new({});

        $anon->add_methods([
                {
                        name => 'test_accessor',
                        clearer => 1,
                        predicate => 1,
                        get => 1,
                        set => 1,
                        ref => 1,
                        type => 'Str',
                        reftype => 1,
                        default => 'xyz',
                },
                {
                        name => 'test_accessor2',
                        set => 1,
                        type => 'HashRef',
                        default => { a => 1, b => 2 },
                }
        ]);

        $anon->build;

add_method

Add a method to the Anonymous::Object. Expects param $method to be a HashRef.

        my $anon = Anonymous::Object->new({});

        $anon->add_method({
                name => 'test_accessor',
                clearer => 1,
                predicate => 1,
                get => 1,
                set => 1,
                ref => 1,
                reftype => 1,
                type => 'Str',
                default => 'xyz',
        });

        $anon->build;

name

The name of the Anonymous::Object method.

clearer

Generates a clearer method.

        $self->clear_$name;

predicate

Generates a predicate method.

        $self->has_$name;

get

Generates a get method.

        $self->get_$name;

set

Generates a set method.

        $self->set_$name;

ref

Generates a ref method.

        $self->ref_$name;

reftype

Generates a reftype method.

        $self->reftype_$name;

type

Specify a type check for the set method.

autotype

Auto detect types based on the passed default values.

default

Set a default value for the method.

build

Build/Generate the Anonymous::Object. Expects no params.

        $obj->build()

stringify_struct

Stringify a perl data structure. Expects param $struct to be any value including undef.

        $obj->stringify_struct($struct)

add_type

Add a type constaint to the Anonymous::Object. Expects param $value to be a Str.

        $obj->add_type('Str');

identify_type

Identify the type of the passed data. Expects param $value to be any value including undef.

        my $type = $obj->identify_type($data);

ACCESSORS

object_name

get or set object_name.

        $obj->object_name;

        $obj->object_name($value);

default

get or set default.

        $obj->default;

        $obj->default($value);

meta

get or set meta.

        $obj->meta;

        $obj->meta($value);

types

get or set types.

        $obj->types;

        $obj->types($value);

type_library

get or set type_library.

        $obj->type_library;

        $obj->type_library($value);

type_map

get or set type_map.

        $obj->type_map;

        $obj->type_map($value);

AUTHOR

LNATION, <email at lnation.org>

BUGS

Please report any bugs or feature requests to bug-anonymous::object at rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Anonymous-Object. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

        perldoc Anonymous::Object

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

This software is Copyright (c) 2020 by LNATION.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)