diff options
author | Toby Inkster <mail@tobyinkster.co.uk> | 2022-09-14 11:47:40 +0100 |
---|---|---|
committer | Toby Inkster <mail@tobyinkster.co.uk> | 2022-09-14 11:47:40 +0100 |
commit | 21593ce8846cec6e4e81ef435530e48d6f0c1437 (patch) | |
tree | 294c39df663207b64daa766f873446d25e91e643 /lib/Type/Library.pm | |
parent | ac865e2ce6b17214453c6797e45e554c9d6e1603 (diff) |
Forbid adding types/coercions to immutable libraries
Diffstat (limited to 'lib/Type/Library.pm')
-rw-r--r-- | lib/Type/Library.pm | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/lib/Type/Library.pm b/lib/Type/Library.pm index b6979d9f..7c1b8038 100644 --- a/lib/Type/Library.pm +++ b/lib/Type/Library.pm @@ -189,6 +189,8 @@ sub add_type { my $meta = shift->meta; my $class = blessed( $meta ); + _croak( 'Type library is immutable' ) if $meta->{immutable}; + my $type = ref( $_[0] ) =~ /^Type::Tiny\b/ ? $_[0] : blessed( $_[0] ) ? Types::TypeTiny::to_TypeTiny( $_[0] ) @@ -238,6 +240,9 @@ sub type_names { sub add_coercion { my $meta = shift->meta; + my $class = blessed( $meta ); + + _croak( 'Type library is immutable' ) if $meta->{immutable}; require Type::Coercion; my $c = blessed( $_[0] ) ? $_[0] : "Type::Coercion"->new( @_ ); @@ -253,7 +258,6 @@ sub add_coercion { no strict "refs"; no warnings "redefine", "prototype"; - my $class = blessed( $meta ); *{"$class\::$name"} = type_to_coderef( $c ); push @{"$class\::EXPORT_OK"}, $name; push @{ ${"$class\::EXPORT_TAGS"}{'coercions'} ||= [] }, $name; @@ -293,7 +297,7 @@ sub make_immutable { } } - 1; + $meta->{immutable} = 1; } 1; |