I thought I could curry a Boolean native type to set to true or false given a function call, but doesn’t seem to work how I expected
updated with traits
has 'Lock' => (
is => 'ro',
isa => 'Bool',
traits => ['Bool'],
default => 0 ,
reader => 'isLocked',
handles => {
lock => [ set => 1 ],
unlock => [ set => 0 ],
flip => 'toggle',
}
);
I think you’re looking for Moose::Meta::Attribute::Native::Trait::Bool here, specified by
traits => ['Bool'].When all you have is
isa => 'Bool', default => 0, your attribute doesn’t hold an object. You can’t call methods on the number 0, so it can’thandlesanything without help from a native trait.From Moose::Meta::Attribute::Native: “Native delegations allow you to delegate to native Perl data structures as if they were objects.” That means that when you use
handleswith them, they generate special methods that perform certain operations on the attribute other than calling a method on an object stored within the attribute. The Bool native trait providesset,unset, andtogglemethods, which means you can do what you want with: