5 Moose::Manual::Delta - Important Changes in Moose
9 This documents any important or noteworthy changes in Moose, with a
10 focus on backwards. This does duplicate data from the F<Changes> file,
11 but aims to provide more details and when possible workarounds.
13 Besides helping keep up with changes, you can also use this document
14 for finding the lowest version of Moose that supported a given
15 feature. If you encounter a problem and have a solution but don't see
16 it documented here, or think we missed an important feature, please
23 =item Roles have their own default attribute metaclass
25 Previously, when a role was applied to a class, it would use the attribute
26 metaclass defined in the class when copying over the attributes in the role.
27 This was wrong, because for instance, using L<MooseX::FollowPBP> in the class
28 would end up renaming all of the accessors generated by the role, some of which
29 may be being called in the role, causing it to break. Roles now keep track of
30 their own attribute metaclass to use by default when being applied to a class
31 (defaulting to Moose::Meta::Attribute). This is modifiable using
32 L<Moose::Util::MetaRole> by passing the C<applied_attribute> key to the
33 C<role_metaroles> option, as in:
35 Moose::Util::MetaRole::apply_metaroles(
38 attribute => ['My::Meta::Role::Attribute'],
41 applied_attribute => ['My::Meta::Role::Attribute'],
51 =item Configurable stacktraces
53 Classes which use the L<Moose::Error::Default> error class can now have
54 stacktraces disabled by setting the C<MOOSE_ERROR_STYLE> env var to C<croak>.
55 This is experimental, fairly incomplete, and won't work in all cases (because
56 Moose's error system in general is all of these things), but this should allow
57 for reducing at least some of the verbosity in most cases.
65 =item Native Delegations
67 In previous versions of Moose, the Native delegations were created as
68 closures. The generated code was often quite slow compared to doing the same
69 thing by hand. For example, the Array's push delegation ended up doing
72 push @{ $self->$reader() }, @_;
74 If the attribute was created without a reader, the C<$reader> sub reference
75 followed a very slow code path. Even with a reader, this is still slower than
78 Native delegations are now generated as inline code, just like other
79 accessors, so we can access the slot directly.
81 In addition, native traits now do proper constraint checking in all cases. In
82 particular, constraint checking has been improved for array and hash
83 references. Previously, only the I<contained> type (the C<Str> in
84 C<HashRef[Str]>) would be checked when a new value was added to the
85 collection. However, if there was a constraint that applied to the whole
86 value, this was never checked.
88 In addition, coercions are now called on the whole value.
90 The delegation methods now do more argument checking. All of the methods check
91 that a valid number of arguments were passed to the method. In addition, the
92 delegation methods check that the arguments are sane (array indexes, hash
93 keys, numbers, etc.) when applicable. We have tried to emulate the behavior of
94 Perl builtins as much as possible.
96 Finally, triggers are called whenever the value of the attribute is changed by
99 These changes are only likely to break code in a few cases.
101 The inlining code may or may not preserve the original reference when changes
102 are made. In some cases, methods which change the value may replace it
103 entirely. This will break tied values.
105 If you have a typed arrayref or hashref attribute where the type enforces a
106 constraint on the whole collection, this constraint will now be checked. It's
107 possible that code which previously ran without errors will now cause the
108 constraint to fail. However, presumably this is a good thing ;)
110 If you are passing invalid arguments to a delegation which were previously
111 being ignored, these calls will now fail.
113 If your code relied on the trigger only being called for a regular writer,
114 that may cause problems.
116 As always, you are encouraged to test before deploying the latest version of
119 =item Defaults is and default for String, Counter, and Bool
121 A few native traits (String, Counter, Bool) provide default values of "is" and
122 "default" when you created an attribute. Allowing them to provide these values
123 is now deprecated. Supply the value yourself when creating the attribute.
125 =item The C<meta> method
127 Moose and Class::MOP have been cleaned up internally enough to make the
128 C<meta> method that you get by default optional. C<use Moose> and
129 C<use Moose::Role> now can take an additional C<-meta_name> option, which
130 tells Moose what name to use when installing the C<meta> method. Passing
131 C<undef> to this option suppresses generation of the C<meta> method
132 entirely. This should be useful for users of modules which also use a C<meta>
133 method or function, such as L<Curses> or L<Rose::DB::Object>.
141 =item All deprecated features now warn
143 Previously, deprecation mostly consisted of simply saying "X is deprecated" in
144 the Changes file. We were not very consistent about actually warning. Now, all
145 deprecated features still present in Moose actually give a warning. The
146 warning is issued once per calling package. See L<Moose::Deprecated> for more
149 =item You cannot pass C<< coerce => 1 >> unless the attribute's type constraint has a coercion
151 Previously, this was accepted, and it sort of worked, except that if you
152 attempted to set the attribute after the object was created, you would get a
155 Now you will get a warning when you attempt to define the attribute.
157 =item C<no Moose>, C<no Moose::Role>, and C<no Moose::Exporter> no longer unimport strict and warnings
159 This change was made in 1.05, and has now been reverted. We don't know if the
160 user has explicitly loaded strict or warnings on their own, and unimporting
161 them is just broken in that case.
163 =item Reversed logic when defining which options can be changed
165 L<Moose::Meta::Attribute> now allows all options to be changed in an
166 overridden attribute. The previous behaviour required each option to be
167 whitelisted using the C<legal_options_for_inheritance> method. This method has
168 been removed, and there is a new method, C<illegal_options_for_inheritance>,
169 which can now be used to prevent certain options from being changeable.
171 In addition, we only throw an error if the illegal option is actually
172 changed. If the superclass didn't specify this option at all when defining the
173 attribute, the subclass version can still add it as an option.
175 Example of overriding this in an attribute trait:
177 package Bar::Meta::Attribute;
180 has 'my_illegal_option' => (
185 around illegal_options_for_inheritance => sub {
186 return ( shift->(@_), qw/my_illegal_option/ );
195 =item L<Moose::Object/BUILD> methods are now called when calling C<new_object>
197 Previously, C<BUILD> methods would only be called from C<Moose::Object::new>,
198 but now they are also called when constructing an object via
199 C<Moose::Meta::Class::new_object>. C<BUILD> methods are an inherent part of the
200 object construction process, and this should make C<< $meta->new_object >>
201 actually usable without forcing people to use C<< $meta->name->new >>.
203 =item C<no Moose>, C<no Moose::Role>, and C<no Moose::Exporter> now unimport strict and warnings
205 In the interest of having C<no Moose> clean up everything that C<use Moose>
206 does in the calling scope, C<no Moose> (as well as all other
207 L<Moose::Exporter>-using modules) now unimports strict and warnings.
209 =item Metaclass compatibility checking and fixing should be much more robust
211 The L<metaclass compatibility|Moose/METACLASS COMPATIBILITY AND MOOSE> checking
212 and fixing algorithms have been completely rewritten, in both Class::MOP and
213 Moose. This should resolve many confusing errors when dealing with non-Moose
214 inheritance and with custom metaclasses for things like attributes,
215 constructors, etc. For correct code, the only thing that should require a
216 change is that custom error metaclasses must now inherit from
217 L<Moose::Error::Default>.
225 =item Moose::Meta::TypeConstraint::Class is_subtype_of behavior
227 Earlier versions of L<is_subtype_of|Moose::Meta::TypeConstraint::Class/is_subtype_of>
228 would incorrectly return true when called with itself, its own TC name or
229 its class name as an argument. (i.e. $foo_tc->is_subtype_of('Foo') == 1) This
230 behavior was a caused by C<isa> being checked before the class name. The old
231 behavior can be accessed with L<is_type_of|Moose::Meta::TypeConstraint::Class/is_type_of>
239 =item Moose::Meta::Attribute::Native::Trait::Code no longer creates reader methods by default
241 Earlier versions of L<Moose::Meta::Attribute::Native::Trait::Code> created
242 read-only accessors for the attributes it's been applied to, even if you didn't
243 ask for it with C<< is => 'ro' >>. This incorrect behaviour has now been fixed.
251 =item Moose::Util add_method_modifier behavior
253 add_method_modifier (and subsequently the sugar functions Moose::before,
254 Moose::after, and Moose::around) can now accept arrayrefs, with the same
255 behavior as lists. Types other than arrayref and regexp result in an error.
259 =head1 0.93_01 and 0.94
263 =item Moose::Util::MetaRole API has changed
265 The C<apply_metaclass_roles> function is now called C<apply_metaroles>. The
266 way arguments are supplied has been changed to force you to distinguish
267 between metaroles applied to L<Moose::Meta::Class> (and helpers) versus
268 L<Moose::Meta::Role>.
270 The old API still works, but will warn in a future release, and eventually be
273 =item Moose::Meta::Role has real attributes
275 The attributes returned by L<Moose::Meta::Role> are now instances of the
276 L<Moose::Meta::Role::Attribute> class, instead of bare hash references.
278 =item "no Moose" now removes C<blessed> and C<confess>
280 Moose is now smart enough to know exactly what it exported, even when it
281 re-exports functions from other packages. When you unimport Moose, it will
282 remove these functions from your namespace unless you I<also> imported them
283 directly from their respective packages.
285 If you have a C<no Moose> in your code I<before> you call C<blessed> or
286 C<confess>, your code will break. You can either move the C<no Moose> call
287 later in your code, or explicitly import the relevant functions from the
288 packages that provide them.
290 =item L<Moose::Exporter> is smarter about unimporting re-exports
292 The change above comes from a general improvement to L<Moose::Exporter>. It
293 will now unimport any function it exports, even if that function is a
294 re-export from another package.
296 =item Attributes in roles can no longer override class attributes with "+foo"
298 Previously, this worked more or less accidentally, because role attributes
299 weren't objects. This was never documented, but a few MooseX modules took
302 =item The composition_class_roles attribute in L<Moose::Meta::Role> is now a method
304 This was done to make it possible for roles to alter the the list of
305 composition class roles by applying a method modifiers. Previously, this was
306 an attribute and MooseX modules override it. Since that no longer works, this
309 This I<should> be an attribute, so this may switch back to being an attribute
310 in the future if we can figure out how to make this work.
318 =item Calling $object->new() is no longer deprecated
320 We decided to undeprecate this. Now it just works.
322 =item Both C<get_method_map> and C<get_attribute_map> is deprecated
324 These metaclass methods were never meant to be public, and they are both now
325 deprecated. The work around if you still need the functionality they provided
326 is to iterate over the list of names manually.
328 my %fields = map { $_ => $meta->get_attribute($_) } $meta->get_attribute_list;
330 This was actually a change in L<Class::MOP>, but this version of Moose
331 requires a version of L<Class::MOP> that includes said change.
339 =item Added Native delegation for Code refs
341 See L<Moose::Meta::Attribute::Native::Trait::Code> for details.
343 =item Calling $object->new() is deprecated
345 Moose has long supported this, but it's never really been documented, and we
346 don't think this is a good practice. If you want to construct an object from
347 an existing object, you should provide some sort of alternate constructor like
348 C<< $object->clone >>.
350 Calling C<< $object->new >> now issues a warning, and will be an error in a
353 =item Moose no longer warns if you call C<make_immutable> for a class with mutable ancestors
355 While in theory this is a good thing to warn about, we found so many
356 exceptions to this that doing this properly became quite problematic.
364 =item New Native delegation methods from L<List::Util> and L<List::MoreUtils>
366 In particular, we now have C<reduce>, C<shuffle>, C<uniq>, and C<natatime>.
368 =item The Moose::Exporter with_caller feature is now deprecated
370 Use C<with_meta> instead. The C<with_caller> option will start warning in a
373 =item Moose now warns if you call C<make_immutable> for a class with mutable ancestors
375 This is dangerous because modifying a class after a subclass has been
376 immutabilized will lead to incorrect results in the subclass, due to inlining,
377 caching, etc. This occasionally happens accidentally, when a class loads one
378 of its subclasses in the middle of its class definition, so pointing out that
379 this may cause issues should be helpful. Metaclasses (classes that inherit
380 from L<Class::MOP::Object>) are currently exempt from this check, since at the
381 moment we aren't very consistent about which metaclasses we immutabilize.
383 =item C<enum> and C<duck_type> now take arrayrefs for all forms
385 Previously, calling these functions with a list would take the first element of
386 the list as the type constraint name, and use the remainder as the enum values
387 or method names. This makes the interface inconsistent with the anon-type forms
388 of these functions (which must take an arrayref), and a free-form list where
389 the first value is sometimes special is hard to validate (and harder to give
390 reasonable error messages for). These functions have been changed to take
391 arrayrefs in all their forms - so, C<< enum 'My::Type' => [qw(foo bar)] >> is
392 now the preferred way to create an enum type constraint. The old syntax still
393 works for now, but it will hopefully be deprecated and removed in a future
400 L<Moose::Meta::Attribute::Native> has been moved into the Moose core from
401 L<MooseX::AttributeHelpers>. Major changes include:
405 =item C<traits>, not C<metaclass>
407 Method providers are only available via traits.
409 =item C<handles>, not C<provides> or C<curries>
411 The C<provides> syntax was like core Moose C<< handles => HASHREF >>
412 syntax, but with the keys and values reversed. This was confusing,
413 and AttributeHelpers now uses C<< handles => HASHREF >> in a way that
414 should be intuitive to anyone already familiar with how it is used for
417 The C<curries> functionality provided by AttributeHelpers has been
418 generalized to apply to all cases of C<< handles => HASHREF >>, though
419 not every piece of functionality has been ported (currying with a
420 CODEREF is not supported).
422 =item C<empty> is now C<is_empty>, and means empty, not non-empty
424 Previously, the C<empty> method provided by Arrays and Hashes returned true if
425 the attribute was B<not> empty (no elements). Now it returns true if the
426 attribute B<is> empty. It was also renamed to C<is_empty>, to reflect this.
428 =item C<find> was renamed to C<first>, and C<first> and C<last> were removed
430 L<List::Util> refers to the functionality that we used to provide under C<find>
431 as L<first|List::Util/first>, so that will likely be more familiar (and will
432 fit in better if we decide to add more List::Util functions). C<first> and
433 C<last> were removed, since their functionality is easily duplicated with
436 =item Helpers that take a coderef of one argument now use C<$_>
438 Subroutines passed as the first argument to C<first>, C<map>, and C<grep> now
439 receive their argument in C<$_> rather than as a parameter to the subroutine.
440 Helpers that take a coderef of two or more arguments remain using the argument
441 list (there are technical limitations to using C<$a> and C<$b> like C<sort>
444 See L<Moose::Meta::Attribute::Native> for the new documentation.
448 The C<alias> and C<excludes> role parameters have been renamed to C<-alias>
449 and C<-excludes>. The old names still work, but new code should use the new
450 names, and eventually the old ones will be deprecated and removed.
454 C<< use Moose -metaclass => 'Foo' >> now does alias resolution, just like
455 C<-traits> (and the C<metaclass> and C<traits> options to C<has>).
457 Added two functions C<meta_class_alias> and C<meta_attribute_alias> to
458 L<Moose::Util>, to simplify aliasing metaclasses and metatraits. This is
459 a wrapper around the old
461 package Moose::Meta::Class::Custom::Trait::FooTrait;
462 sub register_implementation { 'My::Meta::Trait' }
468 When an attribute generates I<no> accessors, we now warn. This is to help
469 users who forget the C<is> option. If you really do not want any accessors,
470 you can use C<< is => 'bare' >>. You can maintain back compat with older
471 versions of Moose by using something like:
473 ($Moose::VERSION >= 0.84 ? is => 'bare' : ())
475 When an accessor overwrites an existing method, we now warn. To work around
476 this warning (if you really must have this behavior), you can explicitly
477 remove the method before creating it as an accessor:
481 __PACKAGE__->meta->remove_method('foo');
487 When an unknown option is passed to C<has>, we now warn. You can silence
488 the warning by fixing your code. :)
490 The C<Role> type has been deprecated. On its own, it was useless,
491 since it just checked C<< $object->can('does') >>. If you were using
492 it as a parent type, just call C<role_type('Role::Name')> to create an
493 appropriate type instead.
497 C<use Moose::Exporter;> now imports C<strict> and C<warnings> into packages
502 C<DEMOLISHALL> and C<DEMOLISH> now receive an argument indicating whether or
503 not we are in global destruction.
507 Type constraints no longer run coercions for a value that already matches the
508 constraint. This may affect some (arguably buggy) edge case coercions that
509 rely on side effects in the C<via> clause.
513 L<Moose::Exporter> now accepts the C<-metaclass> option for easily
514 overriding the metaclass (without L<metaclass>). This works for classes
519 Added a C<duck_type> sugar function to L<Moose::Util::TypeConstraints>
520 to make integration with non-Moose classes easier. It simply checks if
521 C<< $obj->can() >> a list of methods.
523 A number of methods (mostly inherited from L<Class::MOP>) have been
524 renamed with a leading underscore to indicate their internal-ness. The
525 old method names will still work for a while, but will warn that the
526 method has been renamed. In a few cases, the method will be removed
527 entirely in the future. This may affect MooseX authors who were using
532 Calling C<subtype> with a name as the only argument now throws an
533 exception. If you want an anonymous subtype do:
535 my $subtype = subtype as 'Foo';
537 This is related to the changes in version 0.71_01.
539 The C<is_needed> method in L<Moose::Meta::Method::Destructor> is now
540 only usable as a class method. Previously, it worked as a class or
541 object method, with a different internal implementation for each
544 The internals of making a class immutable changed a lot in Class::MOP
545 0.78_02, and Moose's internals have changed along with it. The
546 external C<< $metaclass->make_immutable >> method still works the same
551 A mutable class accepted C<< Foo->new(undef) >> without complaint,
552 while an immutable class would blow up with an unhelpful error. Now,
553 in both cases we throw a helpful error instead.
555 This "feature" was originally added to allow for cases such as this:
563 return My::Class->new($args);
565 But we decided this is a bad idea and a little too magical, because it
566 can easily mask real errors.
570 Calling C<type> or C<subtype> without the sugar helpers (C<as>,
571 C<where>, C<message>) is now deprecated.
573 As a side effect, this meant we ended up using Perl prototypes on
574 C<as>, and code like this will no longer work:
576 use Moose::Util::TypeConstraints;
577 use Declare::Constraints::Simple -All;
579 subtype 'ArrayOfInts'
581 => IsArrayRef(IsInt);
583 Instead it must be changed to this:
588 where => IsArrayRef(IsInt)
592 If you want to maintain backwards compat with older versions of Moose,
593 you must explicitly test Moose's C<VERSION>:
595 if ( Moose->VERSION < 0.71_01 ) {
596 subtype 'ArrayOfInts'
598 => IsArrayRef(IsInt);
604 where => IsArrayRef(IsInt)
611 We no longer pass the meta-attribute object as a final argument to
612 triggers. This actually changed for inlined code a while back, but the
613 non-inlined version and the docs were still out of date.
615 If by some chance you actually used this feature, the workaround is
616 simple. You fetch the attribute object from out of the C<$self>
617 that is passed as the first argument to trigger, like so:
623 my ( $self, $value ) = @_;
624 my $attr = $self->meta->find_attribute_by_name('foo');
632 If you created a subtype and passed a parent that Moose didn't know
633 about, it simply ignored the parent. Now it automatically creates the
634 parent as a class type. This may not be what you want, but is less
637 You could declare a name with subtype such as "Foo!Bar". Moose would
638 accept this allowed, but if you used it in a parameterized type such
639 as "ArrayRef[Foo!Bar]" it wouldn't work. We now do some vetting on
640 names created via the sugar functions, so that they can only contain
641 alphanumerics, ":", and ".".
645 Methods created via an attribute can now fulfill a C<requires>
646 declaration for a role. Honestly we don't know why Stevan didn't make
647 this work originally, he was just insane or something.
649 Stack traces from inlined code will now report the line and file as
650 being in your class, as opposed to in Moose guts.
654 When a class does not provide all of a role's required methods, the
655 error thrown now mentions all of the missing methods, as opposed to
656 just the first missing method.
658 Moose will no longer inline a constructor for your class unless it
659 inherits its constructor from Moose::Object, and will warn when it
660 doesn't inline. If you want to force inlining anyway, pass
661 C<< replace_constructor => 1 >> to C<make_immutable>.
663 If you want to get rid of the warning, pass C<< inline_constructor =>
668 Removed the (deprecated) C<make_immutable> keyword.
670 Removing an attribute from a class now also removes delegation
671 (C<handles>) methods installed for that attribute. This is correct
672 behavior, but if you were wrongly relying on it you might get bit.
676 Roles now add methods by calling C<add_method>, not
677 C<alias_method>. They make sure to always provide a method object,
678 which will be cloned internally. This means that it is now possible to
679 track the source of a method provided by a role, and even follow its
680 history through intermediate roles. This means that methods added by
681 a role now show up when looking at a class's method list/map.
683 Parameter and Union args are now sorted, this makes Int|Str the same
684 constraint as Str|Int. Also, incoming type constraint strings are
685 normalized to remove all whitespace differences. This is mostly for
686 internals and should not affect outside code.
688 L<Moose::Exporter> will no longer remove a subroutine that the
689 exporting package re-exports. Moose re-exports the Carp::confess
690 function, among others. The reasoning is that we cannot know whether
691 you have also explicitly imported those functions for your own use, so
692 we err on the safe side and always keep them.
696 C<Moose::init_meta> should now be called as a method.
698 New modules for extension writers, L<Moose::Exporter> and
699 L<Moose::Util::MetaRole>.
703 Implemented metaclass traits (and wrote a recipe for it):
705 use Moose -traits => 'Foo'
707 This should make writing small Moose extensions a little
712 Fixed C<coerce> to accept anon types just like C<subtype> can.
715 coerce $some_anon_type => from 'Str' => via { ... };
719 Added C<BUILDARGS>, a new step in C<< Moose::Object->new() >>.
723 Fixed how the C<< is => (ro|rw) >> works with custom defined
724 C<reader>, C<writer> and C<accessor> options. See the below table for
727 is => ro, writer => _foo # turns into (reader => foo, writer => _foo)
728 is => rw, writer => _foo # turns into (reader => foo, writer => _foo)
729 is => rw, accessor => _foo # turns into (accessor => _foo)
730 is => ro, accessor => _foo # error, accesor is rw
734 The C<before/around/after> method modifiers now support regexp
735 matching of method names. NOTE: this only works for classes, it is
736 currently not supported in roles, but, ... patches welcome.
738 The C<has> keyword for roles now accepts the same array ref form that
739 L<Moose>.pm does for classes.
741 A trigger on a read-only attribute is no longer an error, as it's
742 useful to trigger off of the constructor.
744 Subtypes of parameterizable types now are parameterizable types
749 Fixed issue where C<DEMOLISHALL> was eating the value in C<$@>, and so
750 not working correctly. It still kind of eats them, but so does vanilla
755 Inherited attributes may now be extended without restriction on the
756 type ('isa', 'does').
758 The entire set of Moose::Meta::TypeConstraint::* classes were
759 refactored in this release. If you were relying on their internals you
760 should test your code carefully.
764 Documenting the use of '+name' with attributes that come from recently
765 composed roles. It makes sense, people are using it, and so why not
766 just officially support it.
768 The C<< Moose::Meta::Class->create >> method now supports roles.
770 It is now possible to make anonymous enum types by passing C<enum> an
771 array reference instead of the C<< enum $name => @values >>.
775 Added the C<make_immutable> keyword as a shortcut to calling
776 C<make_immutable> on the meta object. This eventually got removed!
778 Made C<< init_arg => undef >> work in Moose. This means "do not accept
779 a constructor parameter for this attribute".
781 Type errors now use the provided message. Prior to this release they
786 Moose is now a postmodern object system :)
788 The Role system was completely refactored. It is 100% backwards
789 compat, but the internals were totally changed. If you relied on the
790 internals then you are advised to test carefully.
792 Added method exclusion and aliasing for Roles in this release.
794 Added the L<Moose::Util::TypeConstraints::OptimizedConstraints>
797 Passing a list of values to an accessor (which is only expecting one
798 value) used to be silently ignored, now it throws an error.
802 Added parameterized types and did a pretty heavy refactoring of the
803 type constraint system.
805 Better framework extendability and better support for "making your own
808 =head1 0.25 or before
810 Honestly, you shouldn't be using versions of Moose that are this old,
811 so many bug fixes and speed improvements have been made you would be
812 crazy to not upgrade.
814 Also, I am tired of going through the Changelog so I am stopping here,
815 if anyone would like to continue this please feel free.
819 Stevan Little E<lt>stevan@iinteractive.comE<gt>
821 =head1 COPYRIGHT AND LICENSE
823 Copyright 2009 by Infinity Interactive, Inc.
825 L<http://www.iinteractive.com>
827 This library is free software; you can redistribute it and/or modify
828 it under the same terms as Perl itself.