On Mar 2, 2011, at 9:23 AM, matthew couchman (JIC) wrote:
> I'd like to subclass a non moose class Math::VectorReal so that I can do this

> But its not working, I guess because of the first caveat on the manual page 
> that says that references must be the same between the moose class and the 
> non-moose class.  I'm a bit lost to be honest, could someone point me in the 
> right direction?

The problem is that Math::VectorReal uses an arrayref for its internal 
structure. It does a bless([] ...), but MooseX::NonMoose likes hashrefs.
I think MooseX::NonMoose::InsideOut should take care of it. Something like this:

package BetterVector;
use Moose;
use MooseX::NonMoose::InsideOut;  # NOTE - different module
use namespace::autoclean;
extends 'Math::VectorReal';

has [qw(x y z)] => (
    isa => 'Num',
    is => 'ro',  # NOTE - 'ro', not 'rw'.... more on this below
);

sub FOREIGNBUILDARGS {
    my ($class, %vals) = @_;
    return map { $vals{$_} } qw(x y z); # NOTE - Math::VR requires args in a 
specific order, but WE shouldn't.
}

no Moose;

package main;
use Modern::Perl;
my $v = BetterVector->new(x => 1, y => 2, z => 0.5);
say $v->length;

This is a very trimmed proof-of-concept that will compile, and will (at least 
mostly) work. You'll note I made the x,y,,z accessors read-only. This is 
because in my limited example, if you change one of those values the 
calculations won't reflect it.  You'll note the Math::VR api makes the x,y,z 
accessors read-only and doesn't seem to support changing them.
If you wanted to, you probably can... but you'll have to do a lot of mucking 
about inside.  Personally, this would prompt to either look for a more suitable 
vector library, or write a new one. ymmv.

- mark

Reply via email to