{
    "mode": "info",
    "parameter": "Moose::Cookbook::Basics::Point_AttributesAndSubclassing",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/info/Moose%3A%3ACookbook%3A%3ABasics%3A%3APoint_AttributesAndSubclassing/json",
    "generated": "2026-07-05T13:51:27Z",
    "synopsis": "package Point;\nuse Moose;\nhas 'x' => (isa => 'Int', is => 'rw', required => 1);\nhas 'y' => (isa => 'Int', is => 'rw', required => 1);\nsub clear {\nmy $self = shift;\n$self->x(0);\n$self->y(0);\n}\npackage Point3D;\nuse Moose;\nextends 'Point';\nhas 'z' => (isa => 'Int', is => 'rw', required => 1);\nafter 'clear' => sub {\nmy $self = shift;\n$self->z(0);\n};\npackage main;\n# hash or hashrefs are ok for the constructor\nmy $point1 = Point->new(x => 5, y => 7);\nmy $point2 = Point->new({x => 5, y => 7});\nmy $point3d = Point3D->new(x => 5, y => 42, z => -5);",
    "sections": {
        "Moose::Cookbook::BMoose::Cookbook::Basics::PointAttributesAndSubclassing(3pm)": {
            "content": "",
            "subsections": []
        },
        "NAME": {
            "content": "Moose::Cookbook::Basics::PointAttributesAndSubclassing - Point and\nPoint3D classes, showing basic attributes and subclassing.\n",
            "subsections": []
        },
        "VERSION": {
            "content": "version 2.2200\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "package Point;\nuse Moose;\n\nhas 'x' => (isa => 'Int', is => 'rw', required => 1);\nhas 'y' => (isa => 'Int', is => 'rw', required => 1);\n\nsub clear {\nmy $self = shift;\n$self->x(0);\n$self->y(0);\n}\n\npackage Point3D;\nuse Moose;\n\nextends 'Point';\n\nhas 'z' => (isa => 'Int', is => 'rw', required => 1);\n\nafter 'clear' => sub {\nmy $self = shift;\n$self->z(0);\n};\n\npackage main;\n\n# hash or hashrefs are ok for the constructor\nmy $point1 = Point->new(x => 5, y => 7);\nmy $point2 = Point->new({x => 5, y => 7});\n\nmy $point3d = Point3D->new(x => 5, y => 42, z => -5);\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This is the classic Point example. It is taken directly from the Perl 6\nApocalypse 12 document, and is similar to the example found in the\nclassic K&R C book as well.\n\nAs with all Perl 5 classes, a Moose class is defined in a package.\nMoose handles turning on \"strict\" and \"warnings\" for us, so all we need\nto do is say \"use Moose\", and no kittens will die.\n\nWhen Moose is loaded, it exports a set of sugar functions into our\npackage. This means that we import some functions which serve as Moose\n\"keywords\". These aren't real language keywords, they're just Perl\nfunctions exported into our package.\n\nMoose automatically makes our package a subclass of Moose::Object.  The\nMoose::Object class provides us with a constructor that respects our\nattributes, as well other features. See Moose::Object for details.\n\nNow, onto the keywords. The first one we see here is \"has\", which\ndefines an instance attribute in our class:\n\nhas 'x' => (isa => 'Int', is => 'rw', required => 1);\n\nThis will create an attribute named \"x\". The \"isa\" parameter says that\nwe expect the value stored in this attribute to pass the type\nconstraint for \"Int\" (1). The accessor generated for this attribute\nwill be read-write.\n\nThe \"required => 1\" parameter means that this attribute must be\nprovided when a new object is created. A point object without\ncoordinates doesn't make much sense, so we don't allow it.\n\nWe have defined our attributes; next we define our methods. In Moose,\nas with regular Perl 5 OO, a method is just a subroutine defined within\nthe package:\n\nsub clear {\nmy $self = shift;\n$self->x(0);\n$self->y(0);\n}\n\nThat concludes the Point class.\n\nNext we have a subclass of Point, Point3D. To declare our superclass,\nwe use the Moose keyword \"extends\":\n\nextends 'Point';\n\nThe \"extends\" keyword works much like \"use base\"/\"use parent\". First,\nit will attempt to load your class if needed. However, unlike \"base\",\nthe \"extends\" keyword will overwrite any previous values in your\npackage's @ISA, where \"use base\" will \"push\" values onto the package's\n@ISA.\n\nIt is my opinion that the behavior of \"extends\" is more intuitive.\n(2).\n\nNext we create a new attribute for Point3D called \"z\".\n\nhas 'z' => (isa => 'Int', is => 'rw', required => 1);\n\nThis attribute is just like Point's \"x\" and \"y\" attributes.\n\nThe \"after\" keyword demonstrates a Moose feature called \"method\nmodifiers\" (or \"advice\" for the AOP inclined):\n\nafter 'clear' => sub {\nmy $self = shift;\n$self->z(0);\n};\n\nWhen \"clear\" is called on a Point3D object, our modifier method gets\ncalled as well. Unsurprisingly, the modifier is called after the real\nmethod.\n\nIn this case, the real \"clear\" method is inherited from Point. Our\nmodifier method receives the same arguments as those passed to the\nmodified method (just $self here).\n\nOf course, using the \"after\" modifier is not the only way to accomplish\nthis. This is Perl, right? You can get the same results with this code:\n\nsub clear {\nmy $self = shift;\n$self->SUPER::clear();\n$self->z(0);\n}\n\nYou could also use another Moose method modifier, \"override\":\n\noverride 'clear' => sub {\nmy $self = shift;\nsuper();\n$self->z(0);\n};\n\nThe \"override\" modifier allows you to use the \"super\" keyword to\ndispatch to the superclass's method in a very Ruby-ish style.\n\nThe choice of whether to use a method modifier, and which one to use,\nis often a question of style as much as functionality.\n\nSince Point inherits from Moose::Object, it will also inherit the\ndefault Moose::Object constructor:\n\nmy $point1 = Point->new(x => 5, y => 7);\nmy $point2 = Point->new({x => 5, y => 7});\n\nmy $point3d = Point3D->new(x => 5, y => 42, z => -5);\n\nThe \"new\" constructor accepts a named argument pair for each attribute\ndefined by the class, which you can provide as a hash or hash\nreference. In this particular example, the attributes are required, and\ncalling \"new\" without them will throw an error.\n\nmy $point = Point->new( x => 5 ); # no y, kaboom!\n\nFrom here on, we can use $point and $point3d just as you would any\nother Perl 5 object. For a more detailed example of what can be done,\nyou can refer to the t/recipes/basicspointattributesandsubclassing.t\ntest file.\n\nMoose Objects are Just Hashrefs\nWhile this all may appear rather magical, it's important to realize\nthat Moose objects are just hash references under the hood (3). For\nexample, you could pass $self to \"Data::Dumper\" and you'd get exactly\nwhat you'd expect.\n\nYou could even poke around inside the object's data structure, but that\nis strongly discouraged.\n\nThe fact that Moose objects are hashrefs means it is easy to use Moose\nto extend non-Moose classes, as long as they too are hash references.\nIf you want to extend a non-hashref class, check out\n\"MooseX::InsideOut\".\n",
            "subsections": []
        },
        "CONCLUSION": {
            "content": "This recipe demonstrates some basic Moose concepts, attributes,\nsubclassing, and a simple method modifier.\n",
            "subsections": []
        },
        "FOOTNOTES": {
            "content": "(1) Moose provides a number of builtin type constraints, of which \"Int\"\nis one. For more information on the type constraint system, see\nMoose::Util::TypeConstraints.\n\n(2) The \"extends\" keyword supports multiple inheritance. Simply pass\nall of your superclasses to \"extends\" as a list:\n\nextends 'Foo', 'Bar', 'Baz';\n\n(3) Moose supports using instance structures other than blessed hash\nreferences (such as glob references - see MooseX::GlobRef).\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Method Modifiers\nThe concept of method modifiers is directly ripped off from CLOS. A\ngreat explanation of them can be found by following this link.\n\n<http://www.gigamonkeys.com/book/object-reorientation-generic-functions.html>\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "o   Stevan Little <stevan@cpan.org>\n\no   Dave Rolsky <autarch@urth.org>\n\no   Jesse Luehrs <doy@cpan.org>\n\no   Shawn M Moore <sartak@cpan.org>\n\no    ' (Yuval Kogman) <nothingmuch@woobling.org>\n\no   Karen Etheridge <ether@cpan.org>\n\no   Florian Ragwitz <rafl@debian.org>\n\no   Hans Dieter Pearcey <hdp@cpan.org>\n\no   Chris Prather <chris@prather.org>\n\no   Matt S Trout <mstrout@cpan.org>\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "This software is copyright (c) 2006 by Infinity Interactive, Inc.\n\nThis is free software; you can redistribute it and/or modify it under\nthe same terms as the Perl 5 programming language system itself.\n\nperl v5.34.0      Moose::Cookbook::Basics::PointAttributesAndSubclassing(3pm)",
            "subsections": []
        }
    },
    "summary": "Moose::Cookbook::Basics::PointAttributesAndSubclassing - Point and Point3D classes, showing basic attributes and subclassing.",
    "flags": [],
    "examples": [],
    "see_also": []
}