Moose::Manual::Delta - Important Changes in Moose |
Moose::Manual::Delta - Important Changes in Moose
version 2.1605
This documents any important or noteworthy changes in Moose, with a focus on things that affect backwards compatibility. This does duplicate data from the Changes file, but aims to provide more details and when possible workarounds.
Besides helping keep up with changes, you can also use this document for finding the lowest version of Moose that supported a given feature. If you encounter a problem and have a solution but don't see it documented here, or think we missed an important feature, please send us a patch.
Class::MOP::Method::Overload
class. This class has been removed, and
overloading is now implemented by the Class::MOP::Overload manpage. Overloading is not
really equivalent to a method, so the former implementation didn't work
properly for various cases.
All of the overloading-related methods for classes and roles have the same names, but those methods now return the Class::MOP::Overload manpage objects.
This works much like the MooseX::Role::WithOverloading manpage, except that we properly detect overloading conflicts during role summation and when applying one role to another. the MooseX::Role::WithOverloading manpage did not do any conflict detection.
If you want to write code that uses overloading and works with previous versions of Moose and this one, upgrade to the MooseX::Role::WithOverloading manpage version 0.15 or greater. That version will detect when Moose itself handles overloading and get out of the way.
%INC
Foo.pm
):
{ package Foo; use Moose; }
# ...
use Foo;
If you're using the MOP, this behavior will occur when the create
(or
create_anon_class
) method is used, but not when the initialize
method
is used.
load_class
on 'IO::Handle'
followed by 'IO::File'
could end up with a broken IO::File
, in some
cases).
Now, Moose uses the same mechanisms as perl itself to figure out if a class is
loaded. A class is considered to be loaded if its entry in %INC
is set. Perl
sets the %INC
entry for you automatically whenever a file is loaded via
use
or require
. Also, as mentioned above, Moose also now sets the %INC
entry for any classes defined with it, even if they aren't loaded from a
separate file. This does however mean that if you are trying to use Moose with
non-Moose classes defined in the same file, then you will need to set %INC
manually now, where it may have worked in the past. For instance:
{ package My::NonMoose;
sub new { bless {}, shift }
$INC{'My/NonMoose.pm'} = __FILE__; # alternatively: # use Module::Runtime 'module_notional_filename'; # $INC{module_notional_filename(__PACKAGE__)} = __FILE__; }
{ package My::Moose; use Moose;
extends 'My::NonMoose'; }
If you don't do this, you will get an error message about not being able to
locate My::NonMoose
in @INC
. We hope that this case will be fairly rare.
Class::MOP::load_class
, Class::MOP::is_class_loaded
, and
Class::MOP::load_first_existing_class
have been deprecated. They have been
undocumented and discouraged since version 2.0200. You should replace their use
with the corresponding functions in the Class::Load manpage, or just use
the Module::Runtime manpage directly.
enum
and duck_type
have been deprecatedenum
could be called like this:
enum('MyType' => qw(foo bar baz))
This was confusing, however (since it was different from the syntax for
anonymous enum types), and it makes error checking more difficult (since you
can't tell just by looking whether enum('Foo', 'Bar', 'Baz')
was intended to
be a type named Foo
with elements of Bar
and Baz
, or if this was
actually a mistake where someone got the syntax for an anonymous enum type
wrong). This all also applies to duck_type
.
Calling enum
and duck_type
with a list of arguments as described above
has been undocumented since version 0.93, and is now deprecated. You should
replace
enum MyType => qw(foo bar baz);
in your code with
enum MyType => [qw(foo bar baz)];
For learning about the usage of Moose exception objects, read the Moose::Manual::Exceptions manpage. Individual exceptions are documented in the Moose::Manual::Exceptions::Manifest manpage.
This work was funded as part of the GNOME Outreach Program for Women.
Num
type used to accept anything that fits Perl's notion of a number,
which included Inf, NaN, and strings like " 1234 \n"
. We believe that the
type constraint should indicate ``this is a number'', not ``this coerces to a
number''. Therefore, Num now only accepts integers, floating point numbers
(both in decimal notation and exponential notation), 0, .0, 0.0, etc.
If you want the old behavior you can use the LaxNum
type in
the MooseX::Types::LaxNum manpage.
->init_meta
is even less reliable at loading extensionsMooseX::Foo->init_meta(@_)
(and nothing else) from
within your own init_meta
had a decent chance of doing something useful.
This was never supported behavior, and didn't always work anyway. Due to some
implementation adjustments, this now has a smaller chance of doing something
useful, which could break code that was expecting it to continue doing useful
things. Code that does this should instead just call
MooseX::Foo->import({ into => $into })
.
is_subtype_of
and is_a_type_of
methodsenum
type needed to have two or more values. Nobody knew
why, so we fixed it.
<
$class-
get_all_methods >> and $class->find_method_by_name
.
This also means that you can now apply method modifiers to these methods.
optimize_as
parameter to subtype
, but it could also happen if you create a
the Moose::Meta::TypeConstraint manpage object directly.
Use the inlining feature (inline_as
) added in 2.0100 instead.
Class::Load::load_class
and is_class_loaded
have been removedClass::MOP::load_class
and Class::MOP::is_class_loaded
subroutines
are no longer documented, and will cause a deprecation warning in the
future. Moose now uses the Class::Load manpage to provide this functionality, and you
should do so as well.
shallow_clone
method
This affects the optimize_as
sub exported by
the Moose::Util::TypeConstraints manpage. Use inline_as
instead.
This will start warning in the 2.0300 release.
Attribute (foo) does not pass the type constraint because: Validation failed for 'ArrayRef[Int]' with value ARRAY(0x275eed8)
the error message will instead look like
Attribute (foo) does not pass the type constraint because: Validation failed for 'ArrayRef[Int]' with value [ "a" ]
Note that the Devel::PartialDump manpage can't be made a direct dependency at the moment, because it uses Moose itself, but we're considering options to make this easier.
applied_attribute
key to the
role_metaroles
option, as in:
Moose::Util::MetaRole::apply_metaroles( for => __PACKAGE__, class_metaroles => { attribute => ['My::Meta::Role::Attribute'], }, role_metaroles => { applied_attribute => ['My::Meta::Role::Attribute'], }, );
moose-outdated
script, which can be run at any point to list the
modules which are installed that conflict with the installed version of Moose.
After upgrading Moose, running moose-outdated | cpanm
should be sufficient
to ensure that all of the Moose extensions you use will continue to work.
The other part is that Moose's META.json
file will also specify the
conflicts under the x_conflicts
(now x_breaks
) key. We are working with the Perl tool chain
developers to try to get conflicts support added to CPAN clients, and if/when
that happens, the metadata already exists, and so the conflict checking will
become automatic.
The things on the chopping block are:
Class::MOP::Class->get_attribute_map
, <
Class::MOP::Class-
construct_instance >>, and many others. These were
deprecated in the Class::MOP manpage 0.80_01, released on April 5, 2009.
These methods will be removed entirely in Moose 2.0200.
Class::MOP::subname
, Class::MOP::in_global_destruction
, and
the Class::MOP::HAS_ISAREV
constant. The first two were deprecated in 0.84,
and the last in 0.80. Class::MOP 0.84 was released on May 12, 2009.
These functions will be removed entirely in Moose 2.0200.
alias
and excludes
option for role composition-alias
and -excludes
in Moose 0.89, released on
August 13, 2009.
Passing these will throw an error in Moose 2.0200.
apply_metaclass_roles()
function, as well as passing the
for_class
or any key ending in _roles
to apply_metaroles()
. This was
deprecated in Moose 0.93_01, released on January 4, 2010.
These will all throw an error in Moose 2.0200.
type()
or subtype()
as()
, where
,
etc. return). This was deprecated in Moose 0.71_01, released on February 22,
2009.
This will throw an error in Moose 2.0200.
This will be removed entirely in Moose 2.0200.
In between major releases, we will still make minor releases to add new features, fix bugs, update documentation, etc.
MOOSE_ERROR_STYLE
env var to croak
.
This is experimental, fairly incomplete, and won't work in all cases (because
Moose's error system in general is all of these things), but this should allow
for reducing at least some of the verbosity in most cases.
push @{ $self->$reader() }, @_;
If the attribute was created without a reader, the $reader
sub reference
followed a very slow code path. Even with a reader, this is still slower than
it needs to be.
Native delegations are now generated as inline code, just like other accessors, so we can access the slot directly.
In addition, native traits now do proper constraint checking in all cases. In
particular, constraint checking has been improved for array and hash
references. Previously, only the contained type (the Str
in
HashRef[Str]
) would be checked when a new value was added to the
collection. However, if there was a constraint that applied to the whole
value, this was never checked.
In addition, coercions are now called on the whole value.
The delegation methods now do more argument checking. All of the methods check that a valid number of arguments were passed to the method. In addition, the delegation methods check that the arguments are sane (array indexes, hash keys, numbers, etc.) when applicable. We have tried to emulate the behavior of Perl builtins as much as possible.
Finally, triggers are called whenever the value of the attribute is changed by a Native delegation.
These changes are only likely to break code in a few cases.
The inlining code may or may not preserve the original reference when changes are made. In some cases, methods which change the value may replace it entirely. This will break tied values.
If you have a typed arrayref or hashref attribute where the type enforces a constraint on the whole collection, this constraint will now be checked. It's possible that code which previously ran without errors will now cause the constraint to fail. However, presumably this is a good thing ;)
If you are passing invalid arguments to a delegation which were previously being ignored, these calls will now fail.
If your code relied on the trigger only being called for a regular writer, that may cause problems.
As always, you are encouraged to test before deploying the latest version of Moose to production.
meta
methodmeta
method that you get by default optional. use Moose
and
use Moose::Role
now can take an additional -meta_name
option, which
tells Moose what name to use when installing the meta
method. Passing
undef
to this option suppresses generation of the meta
method
entirely. This should be useful for users of modules which also use a meta
method or function, such as Curses or the Rose::DB::Object manpage.
coerce => 1
unless the attribute's type constraint has a coercionNow you will get a warning when you attempt to define the attribute.
no Moose
, no Moose::Role
, and no Moose::Exporter
no longer unimport strict and warningslegal_options_for_inheritance
method. This method has
been removed, and there is a new method, illegal_options_for_inheritance
,
which can now be used to prevent certain options from being changeable.
In addition, we only throw an error if the illegal option is actually changed. If the superclass didn't specify this option at all when defining the attribute, the subclass version can still add it as an option.
Example of overriding this in an attribute trait:
package Bar::Meta::Attribute; use Moose::Role;
has 'my_illegal_option' => ( isa => 'CodeRef', is => 'rw', );
around illegal_options_for_inheritance => sub { return ( shift->(@_), qw/my_illegal_option/ ); };
new_object
BUILD
methods would only be called from Moose::Object::new
,
but now they are also called when constructing an object via
Moose::Meta::Class::new_object
. BUILD
methods are an inherent part of the
object construction process, and this should make $meta->new_object
actually usable without forcing people to use $meta->name->new
.
no Moose
, no Moose::Role
, and no Moose::Exporter
now unimport strict and warningsno Moose
clean up everything that use Moose
does in the calling scope, no Moose
(as well as all other
the Moose::Exporter manpage-using modules) now unimports strict and warnings.
is_subtype_of('Foo')
== 1) This
behavior was a caused by isa
being checked before the class name. The old
behavior can be accessed with is_type_of
is => 'ro'
. This incorrect behaviour has now been fixed.
apply_metaclass_roles
function is now called apply_metaroles
. The
way arguments are supplied has been changed to force you to distinguish
between metaroles applied to the Moose::Meta::Class manpage (and helpers) versus
the Moose::Meta::Role manpage.
The old API still works, but will warn in a future release, and eventually be removed.
blessed
and confess
If you have a no Moose
in your code before you call blessed
or
confess
, your code will break. You can either move the no Moose
call
later in your code, or explicitly import the relevant functions from the
packages that provide them.
This should be an attribute, so this may switch back to being an attribute in the future if we can figure out how to make this work.
new()
is no longer deprecatedget_method_map
and get_attribute_map
is deprecatedmy %fields = map { $_ => $meta->get_attribute($_) } $meta->get_attribute_list;
This was actually a change in the Class::MOP manpage, but this version of Moose requires a version of the Class::MOP manpage that includes said change.
new()
is deprecated$object->clone
.
Calling $object->new
now issues a warning, and will be an error in a
future release.
make_immutable
for a class with mutable ancestors
reduce
, shuffle
, uniq
, and natatime
.
with_meta
instead. The with_caller
option will start warning in a
future release.
make_immutable
for a class with mutable ancestorsenum
and duck_type
now take arrayrefs for all formsenum 'My::Type' => [qw(foo bar)]
is
now the preferred way to create an enum type constraint. The old syntax still
works for now, but it will hopefully be deprecated and removed in a future
release.
the Moose::Meta::Attribute::Native manpage has been moved into the Moose core from the MooseX::AttributeHelpers manpage. Major changes include:
traits
, not metaclass
handles
, not provides
or curries
provides
syntax was like core Moose handles => HASHREF
syntax, but with the keys and values reversed. This was confusing,
and AttributeHelpers now uses handles => HASHREF
in a way that
should be intuitive to anyone already familiar with how it is used for
other attributes.
The curries
functionality provided by AttributeHelpers has been
generalized to apply to all cases of handles => HASHREF
, though
not every piece of functionality has been ported (currying with a
CODEREF is not supported).
empty
is now is_empty
, and means empty, not non-emptyempty
method provided by Arrays and Hashes returned true if
the attribute was not empty (no elements). Now it returns true if the
attribute is empty. It was also renamed to is_empty
, to reflect this.
find
was renamed to first
, and first
and last
were removedfind
as first, so that will likely be more familiar (and will
fit in better if we decide to add more List::Util functions). first
and
last
were removed, since their functionality is easily duplicated with
curries of get
.
$_
first
, map
, and grep
now
receive their argument in $_
rather than as a parameter to the subroutine.
Helpers that take a coderef of two or more arguments remain using the argument
list (there are technical limitations to using $a
and $b
like sort
does).
See the Moose::Meta::Attribute::Native manpage for the new documentation.
The alias
and excludes
role parameters have been renamed to -alias
and -excludes
. The old names still work, but new code should use the new
names, and eventually the old ones will be deprecated and removed.
use Moose -metaclass => 'Foo'
now does alias resolution, just like
-traits
(and the metaclass
and traits
options to has
).
Added two functions meta_class_alias
and meta_attribute_alias
to
the Moose::Util manpage, to simplify aliasing metaclasses and metatraits. This is
a wrapper around the old
package Moose::Meta::Class::Custom::Trait::FooTrait; sub register_implementation { 'My::Meta::Trait' }
way of doing this.
When an attribute generates no accessors, we now warn. This is to help
users who forget the is
option. If you really do not want any accessors,
you can use is => 'bare'
. You can maintain back compat with older
versions of Moose by using something like:
($Moose::VERSION >= 0.84 ? is => 'bare' : ())
When an accessor overwrites an existing method, we now warn. To work around this warning (if you really must have this behavior), you can explicitly remove the method before creating it as an accessor:
sub foo {}
__PACKAGE__->meta->remove_method('foo');
has foo => ( is => 'ro', );
When an unknown option is passed to has
, we now warn. You can silence
the warning by fixing your code. :)
The Role
type has been deprecated. On its own, it was useless,
since it just checked $object->can('does')
. If you were using
it as a parent type, just call role_type('Role::Name')
to create an
appropriate type instead.
use Moose::Exporter;
now imports strict
and warnings
into packages
that use it.
DEMOLISHALL
and DEMOLISH
now receive an argument indicating whether or
not we are in global destruction.
Type constraints no longer run coercions for a value that already matches the
constraint. This may affect some (arguably buggy) edge case coercions that
rely on side effects in the via
clause.
the Moose::Exporter manpage now accepts the -metaclass
option for easily
overriding the metaclass (without metaclass). This works for classes
and roles.
Added a duck_type
sugar function to the Moose::Util::TypeConstraints manpage
to make integration with non-Moose classes easier. It simply checks if
$obj->can()
a list of methods.
A number of methods (mostly inherited from the Class::MOP manpage) have been renamed with a leading underscore to indicate their internal-ness. The old method names will still work for a while, but will warn that the method has been renamed. In a few cases, the method will be removed entirely in the future. This may affect MooseX authors who were using these methods.
Calling subtype
with a name as the only argument now throws an
exception. If you want an anonymous subtype do:
my $subtype = subtype as 'Foo';
This is related to the changes in version 0.71_01.
The is_needed
method in the Moose::Meta::Method::Destructor manpage is now
only usable as a class method. Previously, it worked as a class or
object method, with a different internal implementation for each
version.
The internals of making a class immutable changed a lot in Class::MOP
0.78_02, and Moose's internals have changed along with it. The
external $metaclass->make_immutable
method still works the same
way.
A mutable class accepted Foo->new(undef)
without complaint,
while an immutable class would blow up with an unhelpful error. Now,
in both cases we throw a helpful error instead.
This ``feature'' was originally added to allow for cases such as this:
my $args;
if ( something() ) { $args = {...}; }
return My::Class->new($args);
But we decided this is a bad idea and a little too magical, because it can easily mask real errors.
Calling type
or subtype
without the sugar helpers (as
,
where
, message
) is now deprecated.
As a side effect, this meant we ended up using Perl prototypes on
as
, and code like this will no longer work:
use Moose::Util::TypeConstraints; use Declare::Constraints::Simple -All;
subtype 'ArrayOfInts' => as 'ArrayRef' => IsArrayRef(IsInt);
Instead it must be changed to this:
subtype( 'ArrayOfInts' => { as => 'ArrayRef', where => IsArrayRef(IsInt) } );
If you want to maintain backwards compat with older versions of Moose,
you must explicitly test Moose's VERSION
:
if ( Moose->VERSION < 0.71_01 ) { subtype 'ArrayOfInts' => as 'ArrayRef' => IsArrayRef(IsInt); } else { subtype( 'ArrayOfInts' => { as => 'ArrayRef', where => IsArrayRef(IsInt) } ); }
We no longer pass the meta-attribute object as a final argument to triggers. This actually changed for inlined code a while back, but the non-inlined version and the docs were still out of date.
If by some chance you actually used this feature, the workaround is
simple. You fetch the attribute object from out of the $self
that is passed as the first argument to trigger, like so:
has 'foo' => ( is => 'ro', isa => 'Any', trigger => sub { my ( $self, $value ) = @_; my $attr = $self->meta->find_attribute_by_name('foo');
# ... } );
If you created a subtype and passed a parent that Moose didn't know about, it simply ignored the parent. Now it automatically creates the parent as a class type. This may not be what you want, but is less broken than before.
You could declare a name with subtype such as ``Foo!Bar''. Moose would accept this allowed, but if you used it in a parameterized type such as ``ArrayRef[Foo!Bar]'' it wouldn't work. We now do some vetting on names created via the sugar functions, so that they can only contain alphanumerics, ``:'', and ``.''.
Methods created via an attribute can now fulfill a requires
declaration for a role. Honestly we don't know why Stevan didn't make
this work originally, he was just insane or something.
Stack traces from inlined code will now report the line and file as being in your class, as opposed to in Moose guts.
When a class does not provide all of a role's required methods, the error thrown now mentions all of the missing methods, as opposed to just the first missing method.
Moose will no longer inline a constructor for your class unless it
inherits its constructor from Moose::Object, and will warn when it
doesn't inline. If you want to force inlining anyway, pass
replace_constructor => 1
to make_immutable
.
If you want to get rid of the warning, pass inline_constructor =>
0
.
Removed the (deprecated) make_immutable
keyword.
Removing an attribute from a class now also removes delegation
(handles
) methods installed for that attribute. This is correct
behavior, but if you were wrongly relying on it you might get bit.
Roles now add methods by calling add_method
, not
alias_method
. They make sure to always provide a method object,
which will be cloned internally. This means that it is now possible to
track the source of a method provided by a role, and even follow its
history through intermediate roles. This means that methods added by
a role now show up when looking at a class's method list/map.
Parameter and Union args are now sorted, this makes Int|Str the same constraint as Str|Int. Also, incoming type constraint strings are normalized to remove all whitespace differences. This is mostly for internals and should not affect outside code.
the Moose::Exporter manpage will no longer remove a subroutine that the exporting package re-exports. Moose re-exports the Carp::confess function, among others. The reasoning is that we cannot know whether you have also explicitly imported those functions for your own use, so we err on the safe side and always keep them.
Moose::init_meta
should now be called as a method.
New modules for extension writers, the Moose::Exporter manpage and the Moose::Util::MetaRole manpage.
Implemented metaclass traits (and wrote a recipe for it):
use Moose -traits => 'Foo'
This should make writing small Moose extensions a little easier.
Fixed coerce
to accept anon types just like subtype
can.
So that you can do:
coerce $some_anon_type => from 'Str' => via { ... };
Added BUILDARGS
, a new step in Moose::Object->new()
.
Fixed how the is => (ro|rw)
works with custom defined
reader
, writer
and accessor
options. See the below table for
details:
is => ro, writer => _foo # turns into (reader => foo, writer => _foo) is => rw, writer => _foo # turns into (reader => foo, writer => _foo) is => rw, accessor => _foo # turns into (accessor => _foo) is => ro, accessor => _foo # error, accesor is rw
The before/around/after
method modifiers now support regexp
matching of method names. NOTE: this only works for classes, it is
currently not supported in roles, but, ... patches welcome.
The has
keyword for roles now accepts the same array ref form that
Moose.pm does for classes.
A trigger on a read-only attribute is no longer an error, as it's useful to trigger off of the constructor.
Subtypes of parameterizable types now are parameterizable types themselves.
Fixed issue where DEMOLISHALL
was eating the value in $@
, and so
not working correctly. It still kind of eats them, but so does vanilla
perl.
Inherited attributes may now be extended without restriction on the type ('isa', 'does').
The entire set of Moose::Meta::TypeConstraint::* classes were refactored in this release. If you were relying on their internals you should test your code carefully.
Documenting the use of '+name' with attributes that come from recently composed roles. It makes sense, people are using it, and so why not just officially support it.
The Moose::Meta::Class->create
method now supports roles.
It is now possible to make anonymous enum types by passing enum
an
array reference instead of the enum $name => @values
.
Added the make_immutable
keyword as a shortcut to calling
make_immutable
on the meta object. This eventually got removed!
Made init_arg => undef
work in Moose. This means ``do not accept
a constructor parameter for this attribute''.
Type errors now use the provided message. Prior to this release they didn't.
Moose is now a postmodern object system :)
The Role system was completely refactored. It is 100% backwards compat, but the internals were totally changed. If you relied on the internals then you are advised to test carefully.
Added method exclusion and aliasing for Roles in this release.
Added the the Moose::Util::TypeConstraints::OptimizedConstraints manpage module.
Passing a list of values to an accessor (which is only expecting one value) used to be silently ignored, now it throws an error.
Added parameterized types and did a pretty heavy refactoring of the type constraint system.
Better framework extensibility and better support for ``making your own Moose''.
Honestly, you shouldn't be using versions of Moose that are this old, so many bug fixes and speed improvements have been made you would be crazy to not upgrade.
Also, I am tired of going through the Changelog so I am stopping here, if anyone would like to continue this please feel free.
This software is copyright (c) 2006 by Infinity Interactive, Inc.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Moose::Manual::Delta - Important Changes in Moose |