Created
August 11, 2020 18:06
-
-
Save tobyink/e8b45e4a59693273fa73b6950f953f4c to your computer and use it in GitHub Desktop.
Mom.pm usage, example 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use strict; | |
| use warnings; | |
| package Widget { | |
| use Mom q{ | |
| name :ro :type(Str) :required | |
| stock_level :rwp :type(Int) :default(0) | |
| sku :ro :type(Str) :required | |
| }; | |
| sub add_stock { | |
| my ( $self, $count ) = ( shift, @_ ); | |
| $self->_set_stock_level( $self->stock_level + $count ); | |
| return $self->stock_level; | |
| } | |
| sub remove_stock { | |
| my ( $self, $count ) = ( shift, @_ ); | |
| croak "out of stock" if $count > $self->stock_level; | |
| $self->_set_stock_level( $self->stock_level - $count ); | |
| return $self->stock_level; | |
| } | |
| } | |
| my $thingy = Widget->new( | |
| name => 'Whatsit', | |
| sku => 'X0000001', | |
| ); | |
| $thingy->add_stock(50); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment