Moose A postmodern metaclass-based object system for Perl 5.

Slides:



Advertisements
Similar presentations
(c) Creative Commons Attribution-ShareAlike License- Jason Purdy CGI::Application Raleigh.PM :: February 21 st, 2005 Jason Purdy
Advertisements

Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
Beginning Moose Houston Perl Mongers, April 10 th 2014.
OO Systems and Roles Curtis "Ovid" Poe
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
The GUS 3.0 Perl Object Layer CBIL Jonathan Schug June
Subroutines Just like C, PERL offers the ability to use subroutines for all the same reasons – Code that you will use over and over again – Breaking large.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Survey of Advanced Perl Topics Database access "OpSys"-like functions Signal Handling Inside-Out Objects.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
1 Exercise /* A lockbox can be open or closed. If closed, only a valid password will open the box. Once the box is open, the contents can be retrieved.
Perl Refernces. Kinds of references: hard: a scalar variable that points to data symbolic: a variable that names another reference typeglob: a kind of.
1 Object Oriented Programming (OOP). Exam Friday Paper questions –6 Subroutines BLAT/BLAST Hash Modules Sorting 2.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Introduction to Perl Yupu Liang cbio at MSKCC
Moose: A Postmodern Object System for Perl Stuart Skelton.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.
Perl Chapter 6 Functions. Subprograms In Perl, all subprograms are functions – returns 0 or 1 value – although may have “side-effects” optional function.
Setting Up TGO User Accounts. Creating User Accounts for Other Users If your company has other users who need to use the Active Orders system, your company’s.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Question of the Day  There are two escalators at each subway stop. One going up & one down.  Whenever an escalator needs to be fixed, they almost always.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Intro to Object Oriented Perl Packages, Modules, Classes.
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Object Oriented Programming Programming Perl Chapter 12: "Objects"
Chapter 2 - OOP Maciej Mensfeld Presented by: Maciej Mensfeld More about OOP dev.mensfeld.pl github.com/mensfeld.
Data sharing and open SEEK. Objectives More encouragement to make Data* open Allowing using Data in publications. Simple control over ISA Elements. Allowing.
BY:- TOPS Technologies
Object Oriented Programming with Perl and Moose
Andrew(amwallis) Classes!
C# for C++ Programmers 1.
Object Oriented Programming
Introduction to Perl: Part II
Zane Moser.
Multi-Methods in Cecil
Perl Reliability Workshop
Perl Modules.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Properties 7: Properties Programming C# © 2003 DevelopMentor, Inc.
Main Idea and Supporting Details.
CS1316: Representing Structure and Behavior
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
Context.
Class Inheritance Concept of Inheritance Java keywords
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Introduction to Objects & Classes
Object-Oriented Programming
CMPE212 – Reminders Assignment 3 due next Friday.
Class Diagram.
More on Creating Classes
CS2013 Lecture 7 John Hurley Cal State LA.
Review: libraries and packages
Subroutines.
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
Engaging families. Creating communities.
CS 240 – Advanced Programming Concepts
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Moose A postmodern metaclass-based object system for Perl 5

Objects in Perl 5 (a short intro) ● A blessed hashref ● Manual new() method ● No real attributes bless {}, __PACKAGE__; sub new { my ( ) my $self = }; # params bless $self, $class; return $self; } sub name { my ( $self, $name ) $name and $self->{'name'} = $name; return $self->{'name'}; }

Why would you want to use Moose? Moose package Person; use strict; use warnings; use Carp qw( confess ); use DateTime; use DateTime::Format::Natural; sub new { my $class = shift; my %p = ref $_[0] ? %{ $_[0] } exists $p{name} or confess 'name is a required attribute'; $class->_validate_name( $p{name} ); exists $p{birth_date} or confess 'birth_date is a required attribute'; $p{birth_date} = $class->_coerce_birth_date( $p{birth_date} ); $class->_validate_birth_date( $p{birth_date} ); $p{shirt_size} = 'l' unless exists $p{shirt_size}: $class->_validate_shirt_size( $p{shirt_size} ); return bless \%p, $class; } sub _validate_name { shift; my $name = shift; local $Carp::CarpLevel = $Carp::CarpLevel + 1; defined $name or confess 'name must be a string'; } Plain old Perl 5 package User; use :Valid; use Moose; use Moose::Util::TypeConstraints; extends 'Person'; subtype ' ' => as 'Str' => where { :Valid->address($_) } => message { "$_ is not a valid address" }; has _address => ( is => 'rw', isa => ' ', required => 1, );

Get it?

Defining an object in Moose ● use strict ● use warnings ● An object! ● new() method ● A pon-.. err.. a moose! package User; use Moose; 1; You get:

Full-Affordance accessors ● ' ro ' is also available for read-only attributes! ● You can set the getter or setter manually via reader / writer ● I'll show more attribute options later on has name => ( is => 'rw', );

An attribute with type constraint ● Lots of types: Str, Int, ArrayRef, HashRef, CodeRef, Regexp ● You can combine: ArrayRef[Str], HashRef[ArrayRef[Int]] ● They have inheritance: Int is a Num ● Roll your own using subtype package User; use Moose; has name => ( is => 'rw', isa => 'Str', ); 1;

Methods are the same as before sub method { my $self = shift;... $self->more(); }

Inheritance is as easy as... ● Multiple inheritance is also possible, extends accepts an array package Punk; use Moose; extends 'Person'; 1; package Child; use Moose; extends qw/ Father Mother /; 1;

Roles are even easier! ● Multiple roles are recommend! with accepts an array too package Punk; use Moose; extends 'Person'; with 'Piercings'; 1; package Punk; use Moose; extends 'Person'; with qw/ Piercings Tattoos /; 1;

More hooks than a coat rack! package User::Secure; use Moose; extends 'User'; before login => sub { my $self = shift; $self- >security_check(); }; 1; ● before ● after ● around ● inner ● augment

Back to attributes options... has set => ( is => 'rw', isa => 'Set::Object', default => sub { Set::Object->new }, required => 1, lazy => 1, predicate => 'has_set', clearer => 'clear_set', builder => 'build_set', );

Attribute options default => 'kitteh', # string default => 3, # number default => sub { {} }, # HashRef default => sub { [] }, # ArrayRef default => sub { Object->new }, # an Object etc. (if you need a more elaborate sub, use builder ) default

Attribute options required => 1, # required required => 0, # not required required

Attribute options lazy => 1, # make it lazy Class will not create the slot for this attribute unless it absolutely has to. That is defined by whether it is accessed at all. Wasn't accessed? You don't pay the penalty! :) lazy = good lazy

Attribute options builder => 'build_it', # subroutine name sub build_it { my $self = shift; # not a problem! return Some::Object->new( $self->more_opts, ); } # and, obviously... after build_it => sub { “they will come” }; (a builder sets the value of the attribute) builder

Attribute options clearer => 'clear_it', # subroutine name # you don't need to create the subroutine sub time_machine { my $self = shift; $self->clear_it; # 'it' never happened :) } (a clearer clears the attribute, not just the value) clearer

Attribute options predicate => 'has_it', # subroutine name # you don't need to create the subroutine sub try_to_do_it { my $self = shift; $self->has_it && $self->do_it(); } (a predicate checks an attribute exists) (even false values) (which is good!) predicate

Attribute options lazy_build => 1, # <3 # the same as: lazy => 1, builder => '_build_it', # private clearer => 'clear_it', predicate => 'has_it', ( lazy_build is recommended) lazy_build

Moose ● It's beautiful ● It's clean ● It's stable ● It's here to stay ● Check it out! ● A ton more options ● More control ● Roles / Traits <3 ● Extensions OMGZ! ● MooseX::POE ● MooseX::Getopt ● MooseX::Declare ● MooseX::Storage ●... sub { goto CPAN; }