<?xml version="1.0" encoding="windows-1252"?>
<node id="221145" title="Re: Class::InsideOut - yet another riff on inside out objects." created="2002-12-19 11:15:41" updated="2005-07-27 14:03:47">
<type id="11">
note</type>
<author id="114691">
Aristotle</author>
<data>
<field name="doctext">
&lt;p&gt;In general, I like your approach - a lot.&lt;/p&gt;

&lt;p&gt;One thing that annoyed me is the &lt;tt&gt;$self-&gt;self&lt;/tt&gt; meme. Confusing, IMO, and not efficient either. I toyed around with the idea of wrapping subs in some way, but haven't come up with any really consistent and watertight semantics. &lt;tt&gt;:-/&lt;/tt&gt; The only possibility would be to tie all field hashes to a class that autocasts any refs used as keys into their &lt;tt&gt;refaddr&lt;/tt&gt; before using them but that doesn't perform any better. Overloading the stringification on the reference might help, but will neither perform better nor work reliably if someone else overloads the same operation. So there really seems to be no other way than to remind everyone to use &lt;tt&gt;$self-&gt;self&lt;/tt&gt; everywhere. Though I'd probably call that &lt;tt&gt;$self-&gt;_id&lt;/tt&gt; instead. Or maybe an attribute &lt;tt&gt;Self&lt;/tt&gt;? Not sure yet.. gonna have to look into that.&lt;/p&gt;

&lt;p&gt;That aside, here's my take on the base class - minus &lt;tt&gt;refaddr&lt;/tt&gt; cause it doesn't work on 5.6.1. What I do is quite simple: store the hashref to the pad &lt;tt&gt;:-)&lt;/tt&gt;. Then all that &lt;tt&gt;AUTOLOAD&lt;/tt&gt; has to do is trawl through the pad hashrefs and look for a matching attribute.&lt;/p&gt;

&lt;code&gt;#!/usr/bin/perl
use strict;
use warnings;

package Class::InsideOut;
use NEXT;
use Attribute::Handlers;
use PadWalker qw(peek_my);
use Data::Dumper;
use vars qw($AUTOLOAD);

my (@Field, %Pad);

sub Field : ATTR(HASH) {
    my ($class, $symbol, $hash) = @_;
    push @Field, $hash;

	my $pad = peek_my(3);
	$Pad{$class}-&gt;{$pad} = peek_my(3);
}

sub AUTOLOAD {
	my $self = $_[0];
	my ($class, $field) = $AUTOLOAD =~ /^(.*)::(.*)$/;

	$field = "%".$field;
	my @field = grep exists $_-&gt;{$field}, values %{$Pad{$class}};

	if(@field) {
		$field = $field[0];
		no strict 'refs';
		*{$AUTOLOAD} = sub {
			my $self = shift;
			@_ ? $field-&gt;{$self} = shift : $field-&gt;{$self};
		};
		goto &amp;{$AUTOLOAD};
	}
	else {
		warn "\@field is empty";
		return $self-&gt;NEXT::AUTOLOAD;
	}
}

sub DESTROY {
    my $self = $_[0];
    delete $_-&gt;{$self} for @Field;
    $self-&gt;NEXT::DESTROY()
}

1;
&lt;/code&gt;

And some test code:

&lt;code&gt;package Foo::Bar;
use base qw(Class::InsideOut);
use Data::Dumper;

my (%foo, %bar, %baz): Field;

package main;

my $x = bless [], 'Foo::Bar';
$x-&gt;foo("bar!\n");

print $x-&gt;foo(), "\n";

__END__
bar!

&lt;/code&gt;

I don't know if it's watertight, though. In particular, how well will it work if I call an accessor for a superclasses' field on a subclass?

&lt;p align="right"&gt;&lt;em&gt;Makeshifts last the longest.&lt;/em&gt;&lt;/p&gt;</field>
<field name="root_node">
220776</field>
<field name="parent_node">
220776</field>
</data>
</node>
