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

Object::FromData::Hash -> Create an object from a hashref.

SYNOPSIS

Don't instantiate directly using this module. Use Object::FromData instead.

    my %hash = (
        name   => 'Bob',
        age    => 27,
        colors => [qw/red green blue/],
    );

    my $hashref = Object::FromData->new( { ref => \%hash } );
    my @keys    = $hashref->keys;
    my @values  = $hashref->values;
    say $hashref->name;    # Bob
    my $arrayref_object = $hashref->colors;    # Object::FromData::Array

is_hashref

    if ( $hashref->is_hashref ) { ... }

Returns true.

is_arrayref

    if ( $hashref->is_arrayref ) { ... }

Returns false.

keys

    my @keys = $hashref->keys;

Returns a list of the keys.

values

    my @values = $hashref->values;

Returns a list of the values.

HASH KEYS OVERRIDING BUILT IN METHODS

We've tried to keep the methods minimal, but because we're inheriting from Object::FromData::Hash and the hash you pass in might have keys which override the main keys. If that happens, call the methods as class methods, passing in the object as an argument:

    my %hash = (
        keys => [qw/foo bar baz/],
    );
    my $object = Object::FromData->new({ ref => \%hash });

    my @keys = $hash->keys; # returns an Object::FromData::Array instance of foo, bar, and baz
    my @keys = Object::FromData::Hash->keys($hash); # returns 'keys'