Moose is an object framework for Perl 5 that simplifies object-oriented programming. It allows classes to be defined declaratively using attributes like 'has' and inheritance is implemented with 'extends'. Attributes can have types, defaults, and delegated accessors. Roles provide reusable traits and are composed into classes using 'with'. Moose supports features like multiple inheritance, method overriding, and required interface methods.
This document provides an introduction and overview of Moose, a modern object framework for Perl 5. It begins with background on the author and a brief history of object oriented programming in Perl 5. It then explains what Moose is, including that it handles object overhead, allows for introspection, and is used in production software. Examples are provided of basic class creation and usage with Moose as well as more advanced features like attributes, types, subclassing, roles, method modifiers, and introspection. The benefits of Moose are summarized as writing less code and avoiding implementation details to have a better object model.
Moose is an object framework for Perl that provides:
1) Full-featured object-oriented programming with attributes, inheritance, roles, and hooks
2) Powerful attribute features like types, defaults, builders, and more
3) A clean and stable API for defining and working with objects
This document introduces object oriented programming in Perl using Moose. It discusses what objects are, how they are commonly created with Moose, and how Moose handles attributes and delegation. Moose provides features like type constraints and immutability out of the box to reduce code and improve performance compared to implementing object oriented code without Moose. The document also covers testing objects and using roles to take on features from multiple packages.
The document discusses how the author evolved the Perlanet software by refactoring it to use Moose and a trait-based architecture. Some key points:
- Perlanet started as a monolithic module but was hard to customize or add new features to.
- The author used Moose to "slash and burn" the code into a more object-oriented and overridable structure using traits instead of large subclasses.
- Traits were used to extract specific behaviors like HTML scrubbing, YAML configuration, etc. into reusable modules.
- This allowed Perlanet to become a "hollow shell" that loads functionality via traits, making it easier to customize and extend.
- The
This is the Moose talk I gave at YAPC::NA 2012.
It included a practical example of a Moose objects code, a simple app called Comican. The code is not available online. If you want it, just email me (sawyer ATT cpan DOTT org).
This document provides an introduction and overview of Moose, a modern object framework for Perl 5. It begins with background on the author and a brief history of object oriented programming in Perl 5. It then explains what Moose is, including that it handles object overhead, allows for introspection, and is used in production software. Examples are provided of basic class creation and usage with Moose as well as more advanced features like attributes, types, subclassing, roles, method modifiers, and introspection. The benefits of Moose are summarized as writing less code and avoiding implementation details to have a better object model.
Moose is an object framework for Perl that provides:
1) Full-featured object-oriented programming with attributes, inheritance, roles, and hooks
2) Powerful attribute features like types, defaults, builders, and more
3) A clean and stable API for defining and working with objects
This document introduces object oriented programming in Perl using Moose. It discusses what objects are, how they are commonly created with Moose, and how Moose handles attributes and delegation. Moose provides features like type constraints and immutability out of the box to reduce code and improve performance compared to implementing object oriented code without Moose. The document also covers testing objects and using roles to take on features from multiple packages.
The document discusses how the author evolved the Perlanet software by refactoring it to use Moose and a trait-based architecture. Some key points:
- Perlanet started as a monolithic module but was hard to customize or add new features to.
- The author used Moose to "slash and burn" the code into a more object-oriented and overridable structure using traits instead of large subclasses.
- Traits were used to extract specific behaviors like HTML scrubbing, YAML configuration, etc. into reusable modules.
- This allowed Perlanet to become a "hollow shell" that loads functionality via traits, making it easier to customize and extend.
- The
This is the Moose talk I gave at YAPC::NA 2012.
It included a practical example of a Moose objects code, a simple app called Comican. The code is not available online. If you want it, just email me (sawyer ATT cpan DOTT org).
Object Oriented got a lot easier since Moose came around.
This keynote is a one-day advanced Moose workshop covering (almost) everything Moose has to offer perl developers
The document provides an overview of modern Perl features including:
- Using say() instead of print for output
- Defined-or operator //
- switch/given statement for conditionals
- Smart matching with ~~ operator
- state keyword for static variables
- New regex features like named capture buffers
- Object oriented programming with Moose
- Defining classes, attributes, types and inheritance with Moose
- Exception handling with TryCatch and autodie
- Best practices for coding style, layout, testing and more
This document provides an introduction and overview of DBIx::Class, an ORM (object relational mapper) for Perl. It discusses setting up tables for an authors and books example database, and performing CRUD (create, read, update, delete) operations both manually using SQL and using DBIx::Class. It also covers creating models with Schema::Loader, debugging, overloading result and result set classes, and inflating/deflating columns.
A helper to make the life of Wordpress developers easier.
This helper can be used to quickly register Custom Post Types, Taxonomies, Meta Boxes, Menu Pages and Sidebars within your Wordpress projects. Please comment, review, watch, fork and report bugs.
You’ve built a WordPress site or two (or 10), your installed plugins and themes to MOSTLY get what you want. Now you’re ready to learn the inner workings of WordPress and take your development to the next level. Jump into WordPress development and PHP by building a Plugin and learn to speak WordPress’ language: PHP.
If your not using an ORM (object relational mapper) and are still writing SQL by hand, here's what you need to know.
An introduction into DBIx::Class and some of the concepts and goodies you should be aware off.
Python's "batteries included" philosophy means that it comes with an astonishing amount of great stuff. On top of that, there's a vibrant world of third-party libraries that help make Python even more wonderful. We'll go on a breezy, example-filled tour through some of my favorites, from treasures in the standard library to great third-party packages that I don't think I could live without, and we'll touch on some of the fuzzier aspects of the Python culture that make it such a joy to be part of.
How else can you write the code in PHP?Maksym Hopei
Usually we are limited with our environment and habits. When we make changes to a product or when we work with a team we follow the coding standards and the existing approaches to make our life and life of our teammates easier.
But often we do the same when we start a new product on our own ruled by our habits and by the limitations of our horizon.
This talk is intended to introduce some uncustomary ways to write code.
This document introduces best practices for writing clean and readable Perl code. It provides examples of poorly formatted code and discusses improvements like using strict and warnings, consistent indentation, descriptive variable names, and limiting line length to 80 characters. The examples demonstrate separating code into logical blocks, spacing around operators, and vertical alignment to improve readability. Adopting these styles and standards helps code be more maintainable as projects evolve over time.
This document provides an introduction and overview of using MongoDB and the Doctrine MongoDB ODM with code examples. It demonstrates basic MongoDB operations like saving, finding, and updating documents. It then shows how the Doctrine MongoDB ODM can be used to map documents to PHP objects to work with them in an object-oriented way, including associations, queries, and lifecycle callbacks. The presenter encourages interested developers to contact them about open positions at OpenSky.
jQuery is drawing newcomers to JavaSCript in droves. As a community, we have an obligation -- and it is in our interest -- to help these newcomers understand where jQuery ends and JavaScript begins.
Banishing Loops with Functional Programming in PHPDavid Hayes
From a talk I've given a few time sat programming conferences about making your code better with functional programming principles. The example code is all PHP
BioPerl is an open source collection of Perl modules for bioinformatics. It contains over 550 modules covering tasks like sequence analysis, multiple sequence alignment, and working with common file formats. The modules provide reusable subroutines and methods to parse data, access databases, and perform other common bioinformatics operations. BioPerl code is portable and can be easily incorporated into scripts and programs. The modules are organized into groups and adhere to object-oriented principles in Perl, with classes, methods, and object blessed references.
My Beginners Perl tutorial, much abbreviated and as presented at the London Perl Workshop on Dec 1st 2007.
Also includes the section on Regular Expressions that we didn't have time for on the day.
Perl, a cross-platform, open-source computer programming language used widely in the commercial and private computing sectors. Perl is a favourite among Web developers for its flexible, continually evolving text-processing and problem-solving capabilities.
This document discusses various data types and data structures in Perl, including scalars, arrays, hashes, references, and object-oriented programming. It provides examples of creating sequences and sequence objects using BioPerl, parsing multi-line sequence data, and accessing GenBank sequence data through the Bio::DB::GenBank module.
Using Moose provides many immediate and obvious benefits, starting with the
obviation of typing "use strict" and "use warnings" in your classes.
The real power of Moose, however, rests in its extensibility. By subclassing
Moose's metaclasses, you can augment and change Moose's behavior to suit your
application's needs. This extensibility is powered by the meta-object protocol
of Moose's foundation, Class::MOP.
The motivating example for extending Moose will be the creation of a small web
framework to empower a Twitter-alike. The focus will be creating meta-level
roles so that metaclasses may select exactly which changes in behavior they
wish to exhibit. Modules that will be used include Moose::Exporter (to define
sugary keywords) and Moose::Util::MetaRole (to extend classes composably).
Experience with using Moose to create regular classes is expected. Having some
familiarity with roles will let you get more out of the talk. No experience
with metaprogramming is required.
This document provides an overview of object-oriented programming concepts and Moose, a object system for Perl 5. It covers introducing OOP, the basics of classes, objects, methods and attributes. It then discusses Moose specifically, including declaring classes and attributes, subclasses, and best practices. The goal is to provide an introduction to OO programming in Perl using Moose.
Object Oriented got a lot easier since Moose came around.
This keynote is a one-day advanced Moose workshop covering (almost) everything Moose has to offer perl developers
The document provides an overview of modern Perl features including:
- Using say() instead of print for output
- Defined-or operator //
- switch/given statement for conditionals
- Smart matching with ~~ operator
- state keyword for static variables
- New regex features like named capture buffers
- Object oriented programming with Moose
- Defining classes, attributes, types and inheritance with Moose
- Exception handling with TryCatch and autodie
- Best practices for coding style, layout, testing and more
This document provides an introduction and overview of DBIx::Class, an ORM (object relational mapper) for Perl. It discusses setting up tables for an authors and books example database, and performing CRUD (create, read, update, delete) operations both manually using SQL and using DBIx::Class. It also covers creating models with Schema::Loader, debugging, overloading result and result set classes, and inflating/deflating columns.
A helper to make the life of Wordpress developers easier.
This helper can be used to quickly register Custom Post Types, Taxonomies, Meta Boxes, Menu Pages and Sidebars within your Wordpress projects. Please comment, review, watch, fork and report bugs.
You’ve built a WordPress site or two (or 10), your installed plugins and themes to MOSTLY get what you want. Now you’re ready to learn the inner workings of WordPress and take your development to the next level. Jump into WordPress development and PHP by building a Plugin and learn to speak WordPress’ language: PHP.
If your not using an ORM (object relational mapper) and are still writing SQL by hand, here's what you need to know.
An introduction into DBIx::Class and some of the concepts and goodies you should be aware off.
Python's "batteries included" philosophy means that it comes with an astonishing amount of great stuff. On top of that, there's a vibrant world of third-party libraries that help make Python even more wonderful. We'll go on a breezy, example-filled tour through some of my favorites, from treasures in the standard library to great third-party packages that I don't think I could live without, and we'll touch on some of the fuzzier aspects of the Python culture that make it such a joy to be part of.
How else can you write the code in PHP?Maksym Hopei
Usually we are limited with our environment and habits. When we make changes to a product or when we work with a team we follow the coding standards and the existing approaches to make our life and life of our teammates easier.
But often we do the same when we start a new product on our own ruled by our habits and by the limitations of our horizon.
This talk is intended to introduce some uncustomary ways to write code.
This document introduces best practices for writing clean and readable Perl code. It provides examples of poorly formatted code and discusses improvements like using strict and warnings, consistent indentation, descriptive variable names, and limiting line length to 80 characters. The examples demonstrate separating code into logical blocks, spacing around operators, and vertical alignment to improve readability. Adopting these styles and standards helps code be more maintainable as projects evolve over time.
This document provides an introduction and overview of using MongoDB and the Doctrine MongoDB ODM with code examples. It demonstrates basic MongoDB operations like saving, finding, and updating documents. It then shows how the Doctrine MongoDB ODM can be used to map documents to PHP objects to work with them in an object-oriented way, including associations, queries, and lifecycle callbacks. The presenter encourages interested developers to contact them about open positions at OpenSky.
jQuery is drawing newcomers to JavaSCript in droves. As a community, we have an obligation -- and it is in our interest -- to help these newcomers understand where jQuery ends and JavaScript begins.
Banishing Loops with Functional Programming in PHPDavid Hayes
From a talk I've given a few time sat programming conferences about making your code better with functional programming principles. The example code is all PHP
BioPerl is an open source collection of Perl modules for bioinformatics. It contains over 550 modules covering tasks like sequence analysis, multiple sequence alignment, and working with common file formats. The modules provide reusable subroutines and methods to parse data, access databases, and perform other common bioinformatics operations. BioPerl code is portable and can be easily incorporated into scripts and programs. The modules are organized into groups and adhere to object-oriented principles in Perl, with classes, methods, and object blessed references.
My Beginners Perl tutorial, much abbreviated and as presented at the London Perl Workshop on Dec 1st 2007.
Also includes the section on Regular Expressions that we didn't have time for on the day.
Perl, a cross-platform, open-source computer programming language used widely in the commercial and private computing sectors. Perl is a favourite among Web developers for its flexible, continually evolving text-processing and problem-solving capabilities.
This document discusses various data types and data structures in Perl, including scalars, arrays, hashes, references, and object-oriented programming. It provides examples of creating sequences and sequence objects using BioPerl, parsing multi-line sequence data, and accessing GenBank sequence data through the Bio::DB::GenBank module.
Using Moose provides many immediate and obvious benefits, starting with the
obviation of typing "use strict" and "use warnings" in your classes.
The real power of Moose, however, rests in its extensibility. By subclassing
Moose's metaclasses, you can augment and change Moose's behavior to suit your
application's needs. This extensibility is powered by the meta-object protocol
of Moose's foundation, Class::MOP.
The motivating example for extending Moose will be the creation of a small web
framework to empower a Twitter-alike. The focus will be creating meta-level
roles so that metaclasses may select exactly which changes in behavior they
wish to exhibit. Modules that will be used include Moose::Exporter (to define
sugary keywords) and Moose::Util::MetaRole (to extend classes composably).
Experience with using Moose to create regular classes is expected. Having some
familiarity with roles will let you get more out of the talk. No experience
with metaprogramming is required.
This document provides an overview of object-oriented programming concepts and Moose, a object system for Perl 5. It covers introducing OOP, the basics of classes, objects, methods and attributes. It then discusses Moose specifically, including declaring classes and attributes, subclasses, and best practices. The goal is to provide an introduction to OO programming in Perl using Moose.
This document discusses how Vim can improve productivity for Perl coding. It provides examples of using Vim motions and modes like Normal mode, Insert mode, and Visual mode to efficiently edit code. It also covers Vim features like syntax highlighting, custom syntax files, key mappings, and text objects that are useful for Perl. The document advocates that Vim is a powerful editor rather than an IDE and highlights how it can save significant time compared to less efficient editing methods.
Great Tools Heavily Used In Japan, You Don't Know.Junichi Ishida
The document discusses Japanese Perl developers who attended YAPC::EU 2015. It introduces many popular Perl modules created by Japanese developers, such as WebService::Simple for making web service requests, Riji for creating blogs, and GrowthForecast for visualizing metrics graphs. It encourages attendees to talk to the Japanese developers about their work or any questions. It emphasizes that Japanese developers prioritize speed and simplicity in their modules due to their culture of valuing efficiency.
.NET Core is a new framework inside .NET Ecosystem. It helps developers to the new challenges of a Cloud First World, and it is not a replacement for the .NET Framework.
Data communication and network Chapter -1Zafar Ayub
This document discusses data communication and networks. It defines data communication as the electronic transmission of digitally encoded information between networks via a medium. A network is defined as hardware, software, and protocols that allow sharing of resources and information according to set rules. The document also defines several key terms related to data communication and networks such as data, resources, channels, protocols, encryption, network hardware and software, senders, and receivers. It describes methods of data transmission including serial and parallel transmission.
This document provides an introduction to data communications and networks. It discusses key topics such as data representation, data flow, characteristics of data communication like delivery and accuracy. It describes different network types including LAN, WAN, MAN. Network topologies like star, bus, ring and hybrid are explained. Protocols define rules for communication regarding what, how and when to communicate. Standards are agreed upon rules and are developed by standards organizations.
An immersive workshop at General Assembly, SF. I typically teach this workshop at General Assembly, San Francisco. To see a list of my upcoming classes, visit https://generalassemb.ly/instructors/seth-familian/4813
I also teach this workshop as a private lunch-and-learn or half-day immersive session for corporate clients. To learn more about pricing and availability, please contact me at http://familian1.com
Part of a series of talk to help you write your first Perl 6 program today. So its basic syntax and concepts of its object orientation and a comparison to the widely used P5 OO system Moose which is similar by no accident.
Elastic::Model is a new framework to store your Moose objects, which uses ElasticSearch as a NoSQL document store and flexible search engine.
It is designed to make small beginnings simple, but to scale easily to Big Data requirements without needing to rearchitect your application. No job too big or small!
This talk will introduce Elastic::Model, demonstrate how to develop a simple application, introduce some more advanced techniques, and discuss how it uses ElasticSearch to scale.
https://github.com/clintongormley/Elastic-Model
Perl is a high-level, general purpose programming language that was introduced in 1987 and remains widely used today. It draws inspiration from languages like C, sed, awk, and grep. The document provides an introduction to Perl's history and basics, including variables, conditionals, loops, regular expressions, subroutines and objects. It highlights advantages like the comprehensive CPAN module library, strong Unicode support, testing culture, and job opportunities. The author works at Nestoria, where Perl powers their property search engine, handling tasks like XML parsing, geocoding, and image processing.
This document provides an overview of Moose, an object system for Perl:
1) Moose allows developers to combine data and functions into classes and objects. It provides features like attributes, accessors, constructors, inheritance, roles, and optimizations that improve performance.
2) Attributes define the data associated with a class and accessors provide read/write interfaces. Constructors initialize objects. Inheritance, roles, and modifiers extend functionality.
3) Moose provides a more robust and standardized way to write object-oriented Perl code compared to Perl's native object system. Over 2,200 modules on CPAN depend on Moose features.
The document discusses Modern Perl and how the language has evolved over time. Some key changes include new features like say() in Perl 5.10, improved object orientation with Moose, and improved error handling with modules like autodie. Modern Perl development focuses on stability while still innovating and uses CPAN to share thousands of open source modules.
This document discusses encoding and decoding JSON objects in Perl. It provides an overview of the JSON module in Perl and describes functions for encoding Perl data structures to JSON strings (encode_json) and decoding JSON strings back to Perl data structures (decode_json). Examples are given showing how to encode a hash and object to JSON and decode a JSON string to a Perl hash.
The document describes a project to build a website called PerkyProfiler that retrieves user profile information from different services like GitHub, Flickr, and Twitter by taking URLs as input. It will use Perl and several Perl modules. The project will be built using the Catalyst web framework and Moose/MooseX for object-oriented programming. It describes using Moose roles, custom types, declarative class definitions, and functional programming techniques in Perl. The goal is to generate a unified user profile by combining data from different services for a given URL.
This document provides a summary of Perl's modern features and history from versions 1 through 5.26. It describes key additions and changes over time such as lexical filehandles in 5.6, three argument open in 5.8, and major new features introduced in 5.10 like say and state. It emphasizes that Perl development is ongoing with thousands of bug fixes and improvements made in each release beyond the new features highlighted.
The document describes an advanced Perl techniques training course that covers new features in Perl 5.10, dates and times using the DateTime module, testing and benchmarking code, and accessing databases. The one-day course will move quickly and cover dates/times, testing, databases, profiling, object-oriented programming with Moose, templates, and MVC frameworks. Resources from the training will be available online.
This document provides an introduction to the Perl programming language for pentesters. It covers fundamental Perl concepts like data types, control structures, loops, functions, file handling and special variables. The document is split into two parts, with the first part focusing on basics and the second part covering more advanced topics like regular expressions, useful modules, and example Perl scripts for pentesting. It also mentions that a future section will demonstrate tools like dnsenum, fierce, nikto and sqlninja.
This document provides an overview of the Ruby programming language. It discusses that Ruby is an object-oriented scripting language created in 1993. It describes some key features of Ruby including being open-source, having a clean syntax, and being able to connect to databases. The document then covers various Ruby programming concepts such as classes, objects, variables, containers, blocks, iterators, and standard data types like numbers, strings, and ranges. It provides examples to illustrate how to use these Ruby features.
- Xslate is a template engine for Perl5 that is written in C using XS. It aims to be fast, safe from XSS attacks, and support multiple template syntaxes including Kolon and TTerse.
- Xslate templates are first preprocessed, parsed into an AST, compiled into bytecode, and then executed by a virtual machine for high performance. Automatic HTML escaping also helps prevent XSS issues.
- Future goals include adding features like loop controls and context controls, as well as exploring more template syntaxes and better integrations with web frameworks.
This document provides an overview of the Python programming language, including its history, key features, syntax examples, and common uses. It also discusses how Python can be used under Linux and some potential issues.
Practical JavaScript Programming - Session 1/8Wilson Su
JavaScript is one of the most popular skills in today’s job market. It allows you to create both client- and server-side applications quickly and easily. Having a solid understanding of this powerful and versatile language is essential to anyone who uses it.
“Practical JavaScript Programming” does not only focus on best practices, but also introduces the fundamental concepts. This course will take you from JavaScript basics to advanced. You’ll learn about topics like Data Types, Functions, Events, AJAX and more.
This article describes how to test Perl programs with the excellent "Test::Class" module. It brings xUnit style testing to Perl. I explain many advanced tips and techniques which even experienced users of "Test::Class" might not be aware of.
The formatting is handled by an experimental piece of software I'm writing which turns Perl's POD format into beautiful postscript files.
One of the advantages of learning a new language is being exposed to new idioms and new approaches to solving old problems. In this talk, we will introduce the Ruby language with particular focus on the idioms and concepts that are different from what is found in Java.
We will introduce concepts such as closures, continuations and meta programming. We will also examine powerful techniques that are practically impossible in Java due to its compile time binding of types.
No experience with Ruby is assumed although an understanding of Java would be helpful.
This talk was given at the Toronto Java Users Group in April 2008
The document discusses how Groovy provides a simpler and more concise way to work with Java code for tasks like file input/output, XML parsing, and configuration compared to Java and other languages like Perl and Ruby. It highlights Groovy features like built-in support for closures, the Elvis operator, date formatting, and ExpandoMetaClass that allow for more readable and expressive code.
3. OO Perl with Moose
What is Moose?
Moose is a postmodern object system for Perl 5 that takes
the tedium out of writing object-oriented Perl. It borrows all
the best features from Perl 6, CLOS (LISP), Smalltalk, Java,
BETA, OCaml, Ruby and more, while still keeping true to its
Perl 5 roots.
5. OO Perl with Moose
Why Moose?
makes Perl 5 OO both simpler and more powerful
6. OO Perl with Moose
Why Moose?
makes Perl 5 OO both simpler and more powerful
define your class declaratively
7. OO Perl with Moose
Why Moose?
makes Perl 5 OO both simpler and more powerful
define your class declaratively
offers "sugar" for object construction, attributes, e.t.c
8. OO Perl with Moose
Why Moose?
makes Perl 5 OO both simpler and more powerful
define your class declaratively
offers "sugar" for object construction, attributes, e.t.c
don't need to care how they are implemented
9. OO Perl with Moose
Why Moose?
makes Perl 5 OO both simpler and more powerful
define your class declaratively
offers "sugar" for object construction, attributes, e.t.c
don't need to care how they are implemented
concentrate on the logical structure of your classes
10. OO Perl with Moose
Why Moose?
makes Perl 5 OO both simpler and more powerful
define your class declaratively
offers "sugar" for object construction, attributes, e.t.c
don't need to care how they are implemented
concentrate on the logical structure of your classes
don't need to be a wizard to use it
11. OO Perl with Moose
Why Moose?
makes Perl 5 OO both simpler and more powerful
define your class declaratively
offers "sugar" for object construction, attributes, e.t.c
don't need to care how they are implemented
concentrate on the logical structure of your classes
don't need to be a wizard to use it
but if you are, lets you dig about in the guts and extend it
12. OO Perl with Moose
package Person; To make a class you start
1;
with a package
13. OO Perl with Moose
package Person; To make a class you start
use Moose;
with a package and just use
1; Moose
14. OO Perl with Moose
package Person; This is a complete class
use Moose;
definition
1;
15. OO Perl with Moose
package Person; This is a complete class
use Moose;
definition
1;
not terribly useful though
16. OO Perl with Moose
package Person; Under the hood Moose is
use Moose;
doing a lot
1;
17. OO Perl with Moose
package Person; Under the hood Moose is
use Moose;
doing a lot
1;
(won't go into that though)
18. OO Perl with Moose
package Person; Classes have zero or more
use Moose;
attributes
1;
19. OO Perl with Moose
package Person; Attributes are declared using
use Moose;
the has function
has 'birth_date' => (
);
1;
20. OO Perl with Moose
package Person; Attributes are declared using
use Moose;
the has function
has 'birth_date' => (
); Attributes have properties
1;
21. OO Perl with Moose
package Person; Attributes are declared using
use Moose;
the has function
has 'birth_date' => (
); Attributes have properties
1;
probably the most powerful
feature of Moose
22. OO Perl with Moose
package Person; Can be provided with
use Moose;
accessors
has 'birth_date' => (
);
1;
23. OO Perl with Moose
package Person; Can be provided with
use Moose;
accessors by stating that
has 'birth_date' => ( attribute is read-writeable
is => 'rw',
);
1;
24. OO Perl with Moose
package Person; or read-only
use Moose;
has 'birth_date' => (
is => 'ro',
);
1;
25. OO Perl with Moose
package Person; or you can provide your own
use Moose;
reader and/or writer
has 'birth_date' => (
is => 'ro',
writer => '_set_birth_date',
);
1;
26. OO Perl with Moose
package Person; You can specify a type for
use Moose;
your attribute
has 'birth_date' => (
is => 'ro',
isa => 'Str',
);
1;
27. OO Perl with Moose
package Person; Only values that pass the
use Moose;
type check will be accepted
has 'birth_date' => ( for the attribute
is => 'ro',
isa => 'Str',
);
1;
28. OO Perl with Moose
package Person; Built in types include:
use Moose;
Str
has 'birth_date' => ( Num
is => 'ro', ArrayRef
isa => 'Str', CodeRef
);
Any
1; more ...
29. OO Perl with Moose
package Person; Class names are treated as
use Moose;
use DateTime;
types
has 'birth_date' => (
is => 'ro',
isa => 'DateTime',
);
1;
30. OO Perl with Moose
package Person;
use Moose; You can create your own
use Moose::Util::TypeConstraints; types
use DateTime;
subtype 'ModernDateTime'
has 'birth_date' => (
is => 'ro',
isa => 'DateTime',
);
1;
31. OO Perl with Moose
package Person;
use Moose; You can create your own
use Moose::Util::TypeConstraints; types
use DateTime;
from existing types
subtype 'ModernDateTime'
=> as 'DateTime'
has 'birth_date' => (
is => 'ro',
isa => 'DateTime',
);
1;
32. OO Perl with Moose
package Person;
use Moose; You can create your own
use Moose::Util::TypeConstraints; types
use DateTime;
from existing types
subtype 'ModernDateTime'
=> as 'DateTime'
=> where { $_->year >= 2000 }; and apply your own
constraints on them
has 'birth_date' => (
is => 'ro',
isa => 'DateTime',
);
1;
33. OO Perl with Moose
package Person;
use Moose; You can create your own
use Moose::Util::TypeConstraints; types
use DateTime;
from existing types
subtype 'ModernDateTime'
=> as 'DateTime'
=> where { $_->year >= 2000 };and apply your own
constraints on them
has 'birth_date' => (
is => 'ro',
isa => 'ModernDateTime', and use them
);
1;
34. OO Perl with Moose
package Person;
use Moose; You can also coerce one
use Moose::Util::TypeConstraints; type into another
use DateTime;
subtype 'ModernDateTime'
=> as 'DateTime'
=> where { $_->year >= 2000 };
has 'birth_date' => (
is => 'ro',
isa => 'ModernDateTime',
);
1;
35. OO Perl with Moose
package Person;
use Moose; See Moose::Manual::Types
use Moose::Util::TypeConstraints; for more details
use DateTime;
subtype 'ModernDateTime'
=> as 'DateTime'
=> where { $_->year >= 2000 };
has 'birth_date' => (
is => 'ro',
isa => 'ModernDateTime',
);
1;
36. OO Perl with Moose
package Person;
use Moose; A person with no birth date
use Moose::Util::TypeConstraints; seems odd
use DateTime;
subtype 'ModernDateTime'
=> as 'DateTime'
=> where { $_->year >= 2000 };
has 'birth_date' => (
is => 'ro',
isa => 'ModernDateTime',
);
1;
37. OO Perl with Moose
package Person;
use Moose; A person with no birth date
use Moose::Util::TypeConstraints; seems odd
use DateTime;
so it can be made
subtype 'ModernDateTime'
=> as 'DateTime' compulsary with the
=> where { $_->year >= 2000 };required flag
has 'birth_date' => (
is => 'ro',
isa => 'ModernDateTime',
required => 1,
);
1;
38. OO Perl with Moose
package Person;
use Moose; You can also set default
use Moose::Util::TypeConstraints; values for the attribute
use DateTime;
subtype 'ModernDateTime'
=> as 'DateTime'
=> where { $_->year >= 2000 };
has 'birth_date' => (
is => 'ro',
isa => 'ModernDateTime',
required => 1,
default => '2000-01-01',
);
1;
39. OO Perl with Moose
package Person;
use Moose; Complex defaults need to
use Moose::Util::TypeConstraints; be set in a sub ref
use DateTime;
subtype 'ModernDateTime'
=> as 'DateTime'
=> where { $_->year >= 2000 };
has 'birth_date' => (
is => 'ro',
isa => 'ModernDateTime',
required => 1,
default => sub { DateTime->now },
);
1;
40. OO Perl with Moose
package Person;
use Moose; or you could write a
separate builder method
# subtype ...
has 'birth_date' => (
is => 'ro',
isa => 'ModernDateTime',
required => 1,
builder => '_build_birth_date',
);
sub _build_birth_date {
DateTime->now;
}
1;
41. OO Perl with Moose
package Person;
use Moose; and make it lazy
# subtype ...
has 'birth_date' => (
is => 'ro',
isa => 'ModernDateTime',
required => 1,
builder => '_build_birth_date',
lazy => 1,
);
sub _build_birth_date {
DateTime->now;
}
1;
42. OO Perl with Moose
package Person;
use Moose; or in one step
# subtype ...
has 'birth_date' => (
is => 'ro',
isa => 'ModernDateTime',
required => 1,
lazy_build => 1,
);
sub _build_birth_date {
DateTime->now;
}
1;
43. OO Perl with Moose
package Person;
use Moose; Attributes handle
delegation
# subtype ...
has 'birth_date' => (
is => 'ro',
isa => 'ModernDateTime',
required => 1,
lazy_build => 1,
handles => { birth_year => 'year' },
);
sub _build_birth_date {
DateTime->now;
}
1;
44. OO Perl with Moose
package Person;
use Moose; Attributes handle
delegation
# subtype ...
has 'birth_date' => ( Calling $person-
is => 'ro',
isa => 'ModernDateTime', >birth_year delegates to
required => 1, $person->birth_date->year
lazy_build => 1,
handles => { birth_year => 'year' },
);
sub _build_birth_date {
DateTime->now;
}
1;
45. OO Perl with Moose
package Person;
use Moose; Delegation is one option to
inheritance
# subtype ...
has 'birth_date' => (
is => 'ro',
isa => 'ModernDateTime',
required => 1,
lazy_build => 1,
handles => { birth_year => 'year' },
);
sub _build_birth_date {
DateTime->now;
}
1;
46. OO Perl with Moose
package Person;
use Moose; Delegation is one option to
inheritance
# subtype ...
has 'birth_date' => ( Especially when inheriting
is => 'ro',
isa => 'ModernDateTime', from non-Moose based
required => 1, classes
lazy_build => 1,
handles => { birth_year => 'year' },
);
sub _build_birth_date {
DateTime->now;
}
1;
47. OO Perl with Moose
package Employee; Inheritance is achieved with
use Moose;
extends qw( Person );
the extends function
1;
48. OO Perl with Moose
package Employee; Inheritance is achieved with
use Moose;
extends qw( Person );
the extends function
1; Moose supports multiple
inheritance
49. OO Perl with Moose
package Employee; Inheritance is achieved with
use Moose;
extends qw( Person );
the extends function
1; Moose supports multiple
inheritance just pass more
class names to extends
50. OO Perl with Moose
package Employee; Override parent methods with
use Moose;
extends qw( Person );
the override function
override '_build_birth_date' => sub {
# ...
}
1;
51. OO Perl with Moose
package Employee; Call the parent method with
use Moose;
extends qw( Person );
the super function
override '_build_birth_date' => sub {
# ...
super();
}
1;
52. OO Perl with Moose
package Employee; You can also override
use Moose;
extends qw( Person );
attributes
has '+birth_date' => (
# ...
);
1;
53. OO Perl with Moose
package Employee; You can also override
use Moose;
extends qw( Person );
attributes (carefully)
has '+birth_date' => (
# ...
);
1;
54. OO Perl with Moose
package Science; Moose also has a concept of
1;
roles
55. OO Perl with Moose
package Science; Moose also has a concept of
use Moose::Role;
roles
1;
Declared by using Moose::
Role
56. OO Perl with Moose
package Science; Similar to Smalltalk traits,
use Moose::Role;
Ruby Mixins and Java
1; interfaces
57. OO Perl with Moose
package Science; Similar to Smalltalk traits,
use Moose::Role;
Ruby Mixins and Java
1; interfaces
Most similar to Perl 6 Roles
58. OO Perl with Moose
package Science; A collection of reusable traits
use Moose::Role;
(attributes)
1;
59. OO Perl with Moose
package Science; A collection of reusable traits
use Moose::Role;
(attributes)
has 'speciality' => (
# ...
);
1;
60. OO Perl with Moose
package Science; A collection of reusable traits
use Moose::Role;
(attributes) and behaviour
has 'speciality' => ( (methods)
# ...
);
sub research {
# ...
}
1;
61. OO Perl with Moose
package Science; Roles are not classes
use Moose::Role;
has 'speciality' => (
# ...
);
sub research {
# ...
}
1;
62. OO Perl with Moose
package Science; Roles are not classes
use Moose::Role;
has 'speciality' => ( cannot instantiate a role
# ...
);
sub research {
# ...
}
1;
63. OO Perl with Moose
package Science; Roles are not classes
use Moose::Role;
has 'speciality' => ( cannot instantiate a role
# ... cannot inherit from a role
);
sub research {
# ...
}
1;
64. OO Perl with Moose
package Science; Roles are another option to
use Moose::Role;
inheritance
has 'speciality' => (
# ...
);
sub research {
# ...
}
1;
65. OO Perl with Moose
package Science; Roles are composed into
use Moose::Role;
consuming classes/roles
has 'speciality' => (
# ...
);
sub research {
# ...
}
1;
66. OO Perl with Moose
package Science; Roles are composed into
use Moose::Role;
consuming classes/roles
has 'speciality' => (
# ... Attributes and methods from
); role are flattened into
sub research { consuming class/role
# ...
}
1;
67. OO Perl with Moose
package Science; Roles can insist that
use Moose::Role;
consuming classes
has 'speciality' => ( implement certain methods
# ...
);
sub research {
# ...
}
1;
68. OO Perl with Moose
package Science; Roles can insist that
use Moose::Role;
consuming classes
requires qw( research ); implement certain methods
has 'speciality' => (
# ... Use the requires function
);
1;
69. OO Perl with Moose
package Science; Roles can insist that
use Moose::Role;
consuming classes
requires qw( research ); implement certain methods
has 'speciality' => (
# ... Use the requires function
);
1;
Consuming classes must
now implement the research
function
70. OO Perl with Moose
package Scientist; Roles are consumed into
use Moose;
extends qw( Person );
classes by using the with
with qw( Science ); keyword
sub research { ... }
1;
71. OO Perl with Moose
package Scientist; Roles are consumed into
use Moose;
extends qw( Person );
classes by using the with
with qw( Science ); keyword
sub research { ... } More than one role can be
1;
consumed into a class
72. OO Perl with Moose
package Scientist; Roles are consumed into
use Moose;
extends qw( Person );
classes by using the with
with qw( Science ); keyword
sub research { ... } More than one role can be
1;
consumed into a class just
pass more roles to with
73. OO Perl with Moose
package Scientist; Class methods and attributes
use Moose;
extends qw( Person );
are prioritized
with qw( Science );
sub research { ... }
1;
74. OO Perl with Moose
package Scientist; Class methods and attributes
use Moose;
extends qw( Person );
are prioritized
with qw( Science );
Conflicts are resolved at
sub research { ... } compile time
1;
75. OO Perl with Moose
package Scientist; See Moose::Manual::Roles
use Moose;
extends qw( Person );
for details
with qw( Science );
sub research { ... }
1;
76. OO Perl with Moose
package Scientist; Moose is not perfect
use Moose;
extends qw( Person );
with qw( Science );
sub research { ... }
1;
77. OO Perl with Moose
package Scientist; Moose is not perfect
use Moose;
extends qw( Person );
with qw( Science ); Biggest caveat is start up
time
sub research { ... }
1;
78. OO Perl with Moose
package Scientist; Moose is not perfect
use Moose;
extends qw( Person );
with qw( Science ); Biggest caveat is start up
time
sub research { ... }
1;
Actively being worked on
79. OO Perl with Moose
package Scientist; Moose is not perfect
use Moose;
extends qw( Person );
with qw( Science ); Biggest caveat is start up
time
sub research { ... }
1;
Actively being worked on
But you can help
80. OO Perl with Moose
package Scientist;
use Moose; Make your classes immutable
extends qw( Person );
with qw( Science );
sub research { ... }
__PACKAGE__->meta->make_immutable();
1;
81. OO Perl with Moose
package Scientist;
use Moose; Make your classes immutable
extends qw( Person );
with qw( Science ); This lets Moose create an
inline constructor for your
sub research { ... }
class
__PACKAGE__->meta->make_immutable();
1;
82. OO Perl with Moose
package Scientist;
use Moose; Make your classes immutable
extends qw( Person );
with qw( Science ); This lets Moose create an
inline constructor for your
sub research { ... }
class
__PACKAGE__->meta->make_immutable();
1;
Greatly speeding up start up
time
83. OO Perl with Moose
package Scientist;
use Moose; Also you are adviced to clean
extends qw( Person ); up after your class
with qw( Science );
i.e remove all Moose sugar
sub research { ... }
__PACKAGE__ from packages using your
->meta->make_immutable(); classes
1;
84. OO Perl with Moose
package Scientist;
use Moose; Also you are adviced to clean
extends qw( Person ); up after your class
with qw( Science );
i.e remove all Moose sugar
sub research { ... }
__PACKAGE__ from packages using your
->meta->make_immutable(); classes
no Moose;
1;
85. OO Perl with Moose
package Scientist;
use Moose; Also you are adviced to clean
use namespace::clean up after your class
-except => [qw( meta )];
extends qw( Person ); i.e remove all Moose sugar
with qw( Science );
from packages using your
sub research { ... } classes
__PACKAGE__
->meta->make_immutable();
1;
Alternatively
87. OO Perl with Moose
Moose is also extensible
Done by manipulating metaclass objects
88. OO Perl with Moose
Moose is also extensible
Done by manipulating metaclass objects
This is where the wizards roam free
89. OO Perl with Moose
Moose is also extensible
Done by manipulating metaclass objects
This is where the wizards roam free
A lot of extensions exist in the MooseX:: namespace
90. OO Perl with Moose
Moose is also extensible
Done by manipulating metaclass objects
This is where the wizards roam free
A lot of extensions exist in the MooseX:: namespace
New ideas usually start life here
91. OO Perl with Moose
Moose is also extensible
Done by manipulating metaclass objects
This is where the wizards roam free
A lot of extensions exist in the MooseX:: namespace
New ideas usually start life here
Good ones get incorporated into Moose
92. OO Perl with Moose
Moose is also extensible
Done by manipulating metaclass objects
This is where the wizards roam free
A lot of extensions exist in the MooseX:: namespace
New ideas usually start life here
Good ones get incorporated into Moose
An example is MooseX::AttributeHelpers
93. OO Perl with Moose
Moose is also extensible
Done by manipulating metaclass objects
This is where the wizards roam free
A lot of extensions exist in the MooseX:: namespace
New ideas usually start life here
Good ones get incorporated into Moose
An example is MooseX::AttributeHelpers
These were incorporated in the Moose::Meta::Attribute::
Native namespace
94. OO Perl with Moose
package Person;
use Moose; Lastly you will note that
use namespace::clean Moose introduces its own
-except => [qw( meta )]; boiler plate code
# attributes and methods
__PACKAGE__->meta->make_immutable();
1;
95. OO Perl with Moose
package Person;
use Moose; Lastly you will note that
use namespace::clean Moose introduces its own
-except => [qw( meta )]; boiler plate code
# attributes and methods
__PACKAGE__->meta->make_immutable(); is an extension
There that
1; reduces this
96. OO Perl with Moose
package Person;
use Moose; Lastly you will note that
use namespace::clean Moose introduces its own
-except => [qw( meta )]; boiler plate code
# attributes and methods
__PACKAGE__->meta->make_immutable(); is an extension
There that
1; reduces this
MooseX::Declare
97. OO Perl with Moose
use MooseX::Declare;
Declaring classes becomes
class Person { even more declarative
# attributes and methods
}
98. OO Perl with Moose
use MooseX::Declare;
Combines the power of
class Person { Moose with Devel::Declare to
# attributes and methods produce keywords for Perl 5
} written in Perl 5
99. OO Perl with Moose
use MooseX::Declare;
Combines the power of
class Person { Moose with Devel::Declare to
# attributes and methods produce keywords for Perl 5
method research() { ... } written in Perl 5
}
Keywords include class, role,
method
100. OO Perl with Moose
use MooseX::Declare;
Combines the power of
class Person { Moose with Devel::Declare to
# attributes and methods produce keywords for Perl 5
method research() { ... } written in Perl 5
}
Keywords include class, role,
method
Note that the methods have
signatures complete with type
checking
101. OO Perl with Moose
use MooseX::Declare;
Combines the power of
class Person { Moose with Devel::Declare to
# attributes and methods produce keywords for Perl 5
method research() { ... } written in Perl 5
}
Keywords include class, role,
method
Note that the methods have
signatures complete with type
checking
So using MooseX::Declare the Scientist class example could look
like the following:
102. OO Perl with Moose
use MooseX::Declare;
class Scientist extends Person with Science {
use Duration; # fictional class one could write
has 'funding' => (
is => 'rw',
isa => 'Num',
lazy_build => 1,
);
method research( Duration $contract_duration ) {
unless ( $self->has_funding ) {
confess 'need funding to work';
}
while ( not $contract_duration->expired ) {
# do your research ...
}
}
}