NAME

object - objects with prototype chains

SYNOPSIS

use object;

# Define class properties (compile time)
object::define('Cat', qw(name age));

# Positional constructor - fastest
my $cat = new Cat 'Whiskers', 3;

# Named pairs constructor
my $cat = new Cat name => 'Whiskers', age => 3;

# Accessors - compiled to custom ops
print $cat->name;        # getter
$cat->age(4);            # setter

# Package methods work normally
package Cat;
sub speak { my $self = shift; "Meow! I am " . $self->name }

package main;
print $cat->speak;       # "Meow! I am Whiskers"
print $cat->isa('Cat');  # true

# Prototype chain
my $proto = $cat->prototype;
$cat->set_prototype($other);

# Mutability controls
$cat->lock;              # Prevent new properties
$cat->unlock;            # Allow new properties again  
$cat->freeze;            # Permanent immutability
$cat->is_frozen;         # Check frozen state

DESCRIPTION

object provides an alternative to bless with prototype chains. Objects are stored as arrays (not hashes) for speed, with property names mapped to slot indices at compile time.

Objects are properly blessed into their class, so isa, can, and custom package methods all work as expected.

Performance

Benchmarks show significant improvements over traditional bless with hash references:

  • Constructor (positional): 35% faster than bless

  • Constructor (named): 17% faster than bless

  • Getter: 143% faster (2.4x) than bless

  • Setter: 107% faster (2x) than bless

FUNCTIONS

object::define($class, @properties)

Define properties for a class at compile time. This assigns slot indices and installs accessor methods. Must be called before using new.

object::define('Cat', qw(name age color));

new $class @args

Create a new object. Supports positional or named arguments.

my $cat = new Cat 'Whiskers', 3;           # positional
my $cat = new Cat name => 'Whiskers';      # named

$obj->prototype

Get the prototype object (or undef if none).

$obj->set_prototype($proto)

Set the prototype object. Fails if object is frozen.

$obj->lock

Prevent adding new properties. Can be unlocked.

$obj->unlock

Allow adding new properties again. Fails if frozen.

$obj->freeze

Permanently prevent modifications. Cannot be undone.

$obj->is_frozen

Returns true if object is frozen.

AUTHOR

LNATION <email@lnation.org>

LICENSE

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