Class::InsideOut 1.01 just hit CPAN.

After seeing Damian's "Sufficiently Advanced Technologies" presentation, I decided to add some automation for common cases. register() now blesses for you and even provides an anonymous scalar reference if you don't give it one. A basic constructor is as simple as this:

sub new { register shift }

Going one step further, an optional and very simple constructor is provided on request that automatically initializes properties from the arguments to new(). A minimal Class::InsideOut based class is now as easy as this:

package My::Class; use Class::InsideOut qw/ new id public private /; public name => my %name; private age => my %age; 1;

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Replies are listed 'Best First'.
Re: Class::InsideOut 1.01 now with added sugar
by clscott (Friar) on Jul 27, 2006 at 21:42 UTC
    Going even another step further why couldn't a minimal Class::InsideOut based class is now as easy as this:
    package My::Class; use Class::InsideOut qw/ new id public private /; public name; private age; 1;
    The age => my %age still seems redundant but kudos on removing a considerable amount of boilerplate!
    --
    Clayton

      Without the lexical hash, it's not an inside-out object. What I left off was actually using the property hashes:

      sub as_string { my $self = shift; return $name{ id $self } . " is " . $age{ id $self } . " year(s) o +ld."; }

      I suppose there's some XS magic that could take a name and create a lexical in the right place, but I'm not an XS expert (yet) and I'm not sure I'd want to add that kind of dependency.

      (Also, in your example those are barewords, so they really should be quoted.)

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

        I think you can reduce public name => my %name; to public \my %name; with the aid of PadWalker’s var_name function. Not sure if you want to depend on that module, though… although OTOH it’s probably better than writing the XS for the purpose yourself.

        Makeshifts last the longest.