STRICT OBJECTS
- Autovivifying can be bad
- Autovivification has been reported as a bug in hashes
-
For cases where you do not want autovivifying you can use strict objects. A strict object will not autovivify.
my $obj = qstrict( foo => 'foo' ); is( $obj->foo, 'foo' ); dies_ok { $obj->bar } "no bar() method defined";
But what if you want to change the specification later?
Easy!
in list context qobj
and qstrict
return both the new object, and a control object that provides a manipulation interface.
my ( $obj, $control ) = qstrict( foo => 'foo' );
$control->set_attributes( bar => 'bar' );
Why not just provide a manipulation interface as a method on the object?
because it might conflict with the api you are attempting to mock.