On Tue, Apr 10, 2012 at 03:47:12PM -0500, Caleb Cushing wrote:
> http://stackoverflow.com/questions/10051181/how-can-i-provide-an-alternate-init-arg-for-an-attribute-in-moose
> 
> I'm trying to add an alternate name/init_arg for some attributes in my
> objects. MooseX::Aliases does this behavior (but with some features I
> don't want, and some things that ultimately make it unusable for what
> I need ). I'm not sure what things I might be missing in my linked
> code to make it work, the trait appears to be added right, but around
> initialize_instance_slot doesn't get called. Which magic have I not
> pulled out of MooseX::Aliases that I need to accomplish this?

I'm still confused as to how this is different than what you need. First
you say "but creating an alias also creates accessors" and then "what I'm
trying to do allow setting of the accessor via the name of the label I'm
adding to the attribute via Meta Recipe3." -- which is exactly what
MooseX::Aliases does.  You appear to want to be able to set the attribute
with the new name *in construction*, as well as *at runtime* via a new
accessor - correct?  so is this not the same as:

has attr => (
    is => 'rw',
    isa => 'Str',
    init_arg => 'attribute',
    accessor => 'attribute',
);

so these tests will hold true:

isnt(exception { Class->new(attr=>'foo') }, undef,
    'cannot construct with the "attr" name');

my $obj;
is(exception { $obj = Class->new(attribute => 'foo') }, undef,
    'can construct with the "attribute" name');
ok(exists($obj->{attr}, 'the "attr" slot is used for storage');
is(exception { $obj->attribute('bar') }, undef,
    'can alter the value with the "attribute" accessor');
ok(!$obj->can('attr'), 'no attr method exists');

If I am wrong, please correct these tests to demonstrate the desired
behaviour.

-- 
        "It is not because things are difficult that we do not dare; it
        is because we do not dare that things are difficult." - Seneca
            .             .            .            .             .
Karen Etheridge, ka...@etheridge.ca       GCS C+++$ USL+++$ P+++$ w--- M++

Reply via email to