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


MooseX::NewDefaults::Magic - A slightly more magical version of NewDefaults


This document describes version 0.004 of MooseX::NewDefaults::Magic - released September 24, 2012 as part of MooseX-NewDefaults.


    package One;
    use Moose;
    use namespace::autoclean;

    has B => (is => 'ro', default => sub { 'say whoo' });
    has C => (is => 'ro', default => sub { 'say bzzi' });

    package Two;
    use Moose;
    use namespace::autoclean;
    use MooseX::NewDefaults::Magic;
    extends 'One';

    # we include the default_for sugar
    default_for B => 'say oooh';

    # magic for defining a new default
    sub C { 'new default' }

    # necessary to invoke the magic


This package applies a metaclass trait that, when the class is made immutable, scans for any methods introduced in that class with the same name as an attribute that exists at some point in the class' ancestry. That method is removed, and the attribute in question is extended to use the removed method as its default.


    # in some package
    has '+foo' => (default => sub { 'a b c' }); the same as:

    # in some package with newdefaults used
    sub foo { 'a b c' }

The default_for() sugar here is exactly the same as shown in MooseX::NewDefaults; please see there for more information.


new_default_for_attribute($attribute_name, [ %options ])

Looks for an attribute called $attribute_name, then for a locally defined method of the same name. If found, removes the local method and uses it as the new default for the attribute.

If called with any %options, it basically just works the same as add_attribute("+$attribute_name" => %options).


Please see those modules/websites for more information related to this module.


The development version is on github at and may be cloned from git://


Please report any bugs or feature requests on the bugtracker website

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.


Chris Weyl <>


This software is Copyright (c) 2011 by Chris Weyl.

This is free software, licensed under:

  The GNU Lesser General Public License, Version 2.1, February 1999