The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

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

VERSION

version 0.003

SYNOPSIS

    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::Mafic;

    # 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
    __PACKAGE__->meta->make_immutable();

DESCRIPTION

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.

e.g.

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

...is the same as:

    # in some package with newdefaults used
    sub foo { 'a b c' }
    __PACKAGE__->meta->make_immutable;

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

NEW METACLASS METHODS

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).

SEE ALSO

MooseX::NewDefaults

BUGS

All complex software has bugs lurking in it, and this module is no exception.

Bugs, feature requests and pull requests through GitHub are most welcome; our page and repo (same URI):

    https://github.com/RsrchBoy/moosex-newdefaults

AUTHOR

Chris Weyl <cweyl@alumni.drew.edu>

COPYRIGHT AND LICENSE

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