NAME

Unknown::Values::Instance::Object - Internal null value object for the "Unknown::Values" distribution

VERSION

version 0.101

SYNOPSIS

package Employee {
    use parent 'Person';
    use Unknown::Values ':OBJECT';

    sub new {
        my ( $class, $id ) = @_;
        my $self = $self->next::method($id) // return unknown;
        ...
        return $self
    }
    
    # ...
}

...

The following assumes that $employee is unknown:

my $employee = Employee->new($id);

# you can call any method on $employee
if ( $employee->salary > $threshold ) {
    ... will never get here if $employee is unknown
}

say $employee->name; # fatal
if ( is_unknown $employee ) {
    ... works as expected
}

if ( $employee->isa('Employee') ) {
    ... isa always returns unknown
}

DESCRIPTION

use Unknown::Values ':OBJECT' implements a variation of the NULL object pattern.

In addition to having all of the behavior of Unknown::Values, you can call any method on the object and it will return the unknown object instances.

Subclassing Unknown Objects

Sometimes you want to provide a default value for a method. You can subclass unknown objects to allow this:

package Unknown::Person {
    use parent 'Unknown::Values::Instance::Object';

    sub name { return '<unknown>' }
}

package Person {
    sub new {
        my ( $class, $name, $age ) = @_;

        if ( not defined $name ) {
            return Unknown::Person->new;
        }
        return bless {
            name => $name,
            age  => $age,
        } => $class;
    }

    sub name { $_[0]->{name} }
    sub age  { $_[0]->{age} }
}

AUTHOR

Curtis "Ovid" Poe <ovid@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2021 by Curtis "Ovid" Poe.

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