![]() Some languages and environments require a formal relationship between two classes before allowing a program to substitute instances for each other. ![]() Instead of passing a value for an age to the constructor, pass in the year of the cat's birth and calculate the age as needed: You can change that if you like, but allowing Moose to manage your storage encourages encapsulation: hiding the internal details of an object from external users of that object.Ĭonsider a change to how Cats manage their ages. Moose itself decides how to store those attributes. Moose allows you to declare which attributes class instances possess (a cat has a name) as well as the attributes of those attributes (you cannot change a cat's name you can only read it). A class describes that data and those behaviors. An object contains related data and can perform behaviors with that data. Instance data begins to demonstrate the value of object orientation. This approach consolidates validation code and ensure that all created objects have valid data. In the Cat example, age() might still be an accessor, but the constructor could take the year of the cat's birth and calculate the age itself based on the current year. Some people suggest making all instance data ro such that you must pass instance data into the constructor ( Immutability). Moose enforces no particular philosophy in this area. Using ro or rw is a matter of design, convenience, and purity. Say $fat->name(), ' now eats ', $fat->diet() Īn ro accessor used as a mutator will throw the exception Cannot assign a value to a read-only accessor at. $fat->diet( 'Low Sodium Kitty Lo Mein' ) Classes use packages ( Packages) to provide namespaces: ClassesĪ Moose object is a concrete instance of a class, which is a template describing data and behavior specific to the object. Moose code interoperates with the default object system and is currently the best way to write object oriented code in modern Perl 5. It provides simpler defaults, and advanced features borrowed from languages such as Smalltalk, Common Lisp, and Perl 6. Moose is a complete object system for Perl 5 See perldoc Moose::Manual for more information. You can build great things on top of it, but it provides little assistance for some basic tasks. Perl 5's default object system is flexible, but minimal. One popular technique is object orientation (OO), or object oriented programming (OOP), where programs work with objects-discrete, unique entities with their own identities. Several techniques group functions into units of related behaviors. Our only hope to manage this complexity is to exploit abstraction (treating similar things similarly) and encapsulation (grouping related details together).įunctions alone are insufficient for large problems. The larger the program, the more details you must manage.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |