The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

OO::InsideOut - Minimal support for Inside-Out Classes

VERSION

0.01

SYNOPSIS

    package My::Name;

    use OO::InsideOut qw(id register);

    register \my( %Name, %Surname );

    sub new {
        my $class = shift;

        return bless \(my $o), ref $class || $class;
    }

    sub name { 
        my $id = id shift;

        scalar @_
            and $Name{ $id } = shift;

        return $Name{ $id };
    }

    sub surname { 
        my $id = id shift;

        scalar @_
            and $Surname{ $id } = shift;

        return $Surname{ $id };
    }

    ...

EXPORT

Nothing by default but every function, in FUNCTIONS, can be exported on demand.

DESCRIPTION

NOTE: If you're developing for perl 5.10 or later, please consider using Hash::Util::FieldHash instead.

OO::InsideOut provides minimal support for Inside-Out Classes for perl 5.8 or later. By minimal, understand;

  • No special methods or attributtes;

  • Don't use source filters or any kind of metalanguage;

  • No need for a special constructor;

  • No need to register objects;

  • No serialization hooks (like Storable, Dumper, etc);

It provides:

  • Automatic object registration;

  • Automatic object destruction;

  • Thread support (but not shared);

  • Foreign inheritance;

  • mod_perl compatibility

FUNCTIONS

id

    id( $object );

Uses Scalar::Util::refaddr to return the reference address of $object.

register

    register( @hashrefs );

Register the given hashrefs for proper cleanup.

Returns an HASH ref with registered objects in the CLASS. See CAVEATS.

Dumper

    Dumper( $object );

If available, uses Data::Dumper::Dumper to dump the object's data.

WARNING: May be removed in the future!!!

HOW IT WORKS

When registering hashes, and only then, OO::InsideOut will:

  • Wrap any new() method*, in the inheritance tree, with the ability to register objects;

  • Wrap any DESTROY() method*, in the inheritance tree, with the ablity to cleanup the object's data;

  • If no DESTROY() method was found, it provides one in the firs package of the inheritance tree;

* This is done only once per package.

PERFORMANCE

Every Inside-Out technique, using an id to identify the object, will be slower than the classic OO approach: it's just the way it is.

Consider:

    sub name {
        my $self = shift;

        scalar @_
            && $Name{ id $self } = shift;

        return $Name{ id $self );
    }

In this example, the code is calling the id twice, causing uncessary overload. If you are going to use id more than once, in the same scope, consider saving it in an variable earlier:

    sub name { 
        my $id = id shift;

        scalar @_
            && $Name{ $id } = shift;

        return $Name{ $id };
    }

MIGRATING TO Hash::Util::FieldHash

Bare in mind that, besides the obvious diferences between the two modules, in Hash::Util::FieldHash, the cleanup process is triggered before calling DESTROY(). In OO::Insideout, this only happens after any DESTROY() defined in the package.

See How to use Field Hashes.

DIAGNOSTICS

must provide, at least, one hash ref!

Besides the obvious reason, this migth happen while using my with a list with only one item:

    register \my( %Field ) #WRONG
    register \my %Field    #RIGTH

CAVEATS

register(), on request, will return an HASH ref with all the objects registered in the CLASS.

If, for any reason, you need to copy/grep this HASH ref, make sure to weaken every entry again. See Scalar::Util::weaken for more detail on this subject.

AUTHOR

André "Rivotti" Casimiro, <rivotti at cpan.org>

BUGS

Please report any bugs or feature requests through the issue tracker at https://github.com/ARivottiC/OO-InsideOut/issues.

SUPPORT

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

    perldoc OO::InsideOut

You can also look for information at:

ACKNOWLEDGEMENTS

SEE ALSO

Alter, Class::InsideOut, Class::Std, Hash::Util::FieldHash, Object::InsideOut.

LICENSE AND COPYRIGHT

Copyright 2013 André Rivotti Casimiro.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.