Moose: A Postmodern Object System for Perl Stuart Skelton.

Slides:



Advertisements
Similar presentations
Advanced Perl WUC Perl Poetry Variable Scope.
Advertisements

Python Objects and Classes
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
C++ Classes & Data Abstraction
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.
Written by: Dr. JJ Shepherd
OBJECT ORIENTED PROGRAMMING (OOP) IN PYTHON David Moodie.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
OOP - Object Oriented Programming Object Oriented Programming is an approach to programming that was developed to make large programs easier to manage.
Section 5 – Classes. Object-Oriented Language Features Abstraction –Abstract or identify the objects involved in the problem Encapsulation –Packaging.
CHAPTER 13 Object Oriented Programming. Objectives  Design class definitions  Implement data hiding and encapsulation  Use accessor and mutator methods.
Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)
Week 10: Objects and Classes 1.  There are two classifications of types in Java  Primitive types:  int  double  boolean  char  Object types: 
Perl Functions Software Tools. Slide 2 Defining a Function l A user-defined function or subroutine is defined in Perl as follows: sub subname{ statement1;
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
Alok Mehta - Programming in Lisp - Data Abstraction and Mapping Programming in Lisp Common Lisp Object System (CLOS)
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
1 Inheritance in Java CS 3331 Fall Outline  Overloading  Inheritance and object initialization  Subtyping  Overriding  Hiding.
PHP Workshop ‹#› PHP Classes and Object Orientation.
CSE S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar.
Abstract classes and Interfaces. Abstract classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Survey of Advanced Perl Topics Database access "OpSys"-like functions Signal Handling Inside-Out Objects.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
ClassesPHPMay-2007 : [‹#›] PHP Classes and Object Orientation.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.
Topic 11: Object-oriented Perl CSE3395 Perl Programming Camel3 chapter 12, pages perlobj, perltoot, perlbot, perlmod manpages.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
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.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Class Builder Tutorial Presented By- Amit Singh & Sylendra Prasad.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Week 12 - Wednesday.  What did we talk about last time?  Hunters and prey.
1 Classes: A class is a concept of something Vehicle 4 wheels, seats, engine, windows, color…… accelerate, start, stop, turn, lock, Truck.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
Written by: Dr. JJ Shepherd
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 8 An Introduction.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
1 C++ Programming Classes ● Class definition and implementation ● Constructors, data members ● Member Functions ● Structuring C + + code.
Moose A postmodern metaclass-based object system for Perl 5.
Object Oriented Programming with Perl and Moose
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Re-Intro to Object Oriented Programming
Reuse Separate classes could be developed for all the different objects that are needed in each program. However that would be wasteful. Often many functionalities.
Objects as a programming concept
Perl Modules.
Object-Oriented Programming
Classes In C#.
PHP Classes and Object Orientation
Inheritance in Java CS 3331 Fall 2009.
Presentation transcript:

Moose: A Postmodern Object System for Perl Stuart Skelton

2 What is OOP? “Traditional” programming is procedural Subroutines work on variables my $distance = distance($point1,$point2); Variables are dumb Just stores for data

3 What is OOP? Object Oriented programming inverts this Variables are objects Objects can carry out certain processes  Called methods my $distance = $point1- >distance($point2); Objects are intelligent Objects know what methods they can carry out

4 Some Concepts A Class is a type of intelligent variable (blueprint of an object)  e.g. Dog An Object is an instance of a class  e.g. Amber An Attribute is a piece of data in an object  e.g. KennelClubName A Method is an action that an object does  e.g. Bark

5 Methods M ethods can be either class methods or object methods Class methods are called on a class  my $amber = Dog->new; Object methods are called on an object  $amber->bark;

6 Constructors All classes need a constructor method Creates a new object of that class Usually a class method Often called new my $amber = Dog->new; my $hunter = new Dog;

7 Accessors & Mutators Access object attributes with an accessor method say “The kennel club name of Amber is “,$amber->get_kennel_club_name; Change an attribute with a mutator method $amber->set_age( $amber->get_age + 1 );

8 Accessor & Mutators Accessors and mutators are often the same method say “The kennel club name of Amber is “, $amber->kennel_club_name; $amber->age($amber->age + 1); Checks number of parameters Reacts appropriately

9 Traditional Perl OOP

10 Basic Anatomy Package Classname; atribute1 attribute2 method1 method2 1;

package PointTP; sub new { my $class = shift; my $self = { x => shift, y => shift, }; bless $self, $class; return $self; } sub set_x { my ( $self, $x ) $self->{x} = $x if defined($x); } sub get_x { my( $self ) return $self->{x}; } sub set_y { my ( $self, $y ) $self->{y} = $y if defined($y); } sub get_y { my( $self ) return $self->{y}; }

#!/usr/bin/perl use Modern::Perl ; use PointTP ; my $point1 = Point->new(1,2) ; say $point1->get_x() ; # prints 1 #change the x-coord $point1->set_x(-10); say $point1->get_x() ; # prints -10

sub set_x { my ( $self, $x ) $self->{x} = $x if defined($x); } sub get_x { my( $self ) return $self->{x}; } sub x { my ($self, $new_x) if (defined $new_x) { $self->{x} = $new_x; } return $self->{x}; }

#!/usr/bin/perl use Modern::Perl ; use Point ; my $point1 = Point->new(1,2) ; say $point1->x() ; # prints 1 #change the x-coord $point1->x(-10); say $point1->x() ; # prints -10

Moose

16 Moose Moose is a Modern Object System for Perl 5 Based on Perl 6 object system More powerful More flexible Easier

17 Simple Moose Class package Point; use Moose; has x => ( is => 'rw', isa => 'Num', required => 1 ); has y => ( is => 'rw', isa => 'Num', required => 1 ); no Moose; __PACKAGE__->meta->make_immutable;

18 What's Going On? use Moose; Loads Moose environment Makes our class a subclass of Moose::Object Turns on use strict and use warnings

19 Declarative Attributes has x => ( is => 'rw', isa => 'Num', required => 1 ); Creates an attribute called 'x' Makes it read/write Must be a number Is required

20 Housekeeping Moose classes carry a lot of baggage We can (and should) turn some of it off no Moose;  Remove Moose exports from your namespace  See also namespace::autoclean __PACKAGE__->meta->make_immutable;  No more changes to class definition Performance improvements

Required Attributes By default Moose attributes are optional Make them mandatory with required has x => (... required => 1, ); my $point1 = Point->new; “Attribute (x) is required at constructor Point::new”

Attribute Defaults Set a default for missing attributes has accuracy => ( default => 0.5, ); Or a subroutine reference has accuracy => ( default => sub { rand }, );

Attribute Builder Define a builder method instead of a default subroutine has accuracy => ( builder => '_build_accuracy', ); sub _build_accuracy { return rand; } Easier to subclass

24 Read/Write Attributes Moose creates methods to access/alter attributes $point1->x(-1); say $point1->x; The 'is' property controls how they work 'rw' : read and write 'ro' : read only

25 Read/Write Attributes But PBP says use get_* and set_* has x => (...., reader => get_x, Writer => set_x, ); use MooseX::FollowPBP; Rule of Thumb, pick a style and stick with it

26 Other Methods Not all methods are constructors or accessors/mutators Write other methods as usual First parameter is object reference

27 Other Methods package Point;... sub distance { my $self = shift ; my $newpoint = shift ; return unless $newpoint->isa('Point') ; return sqrt( ($self->x - $newpoint- >x)**2 + ($self->y - $newpoint- >y)**2 ) ; }

28 Using the Point #!/usr/bin/perl use Modern::Perl ; use Point ; my $point1 = Point->new(x=>-7,y=>- 4) ; my $point2 = Point->new(x=>17,y=>6) ; say join ' ', 'The Distance between point 1', $point1->toString, 'and point 2', $point2->toString, 'is', $point1- >distance($point2) ; # prints 'The Distance between point 1 (-7,-4) and point 2 (17,6) is 26'

Sub Classing A subclass is a specialisation of a superclass More specific behaviour New attributes New methods Overriding superclass methods and attributes

30 package Point3D; use Moose; extends 'Point' ; has z => ( is => 'rw', isa => 'Num', required => 1 ); override distance => sub { my $self = shift; my $newpoint = shift; return unless $newpoint->isa('Point3D') ; my $distance2d = super(); return sqrt( $distance2d**2 + ($self->z - $newpoint->z)**2 ) ; };

31 use Modern::Perl ; use Point3D ; my $point1 = Point3D->new(x=>-7,y=>-4,z=>3); my $point2 = Point3D->new(x=>17,y=>6,z=>2.5); say join ' ', 'The Distance between point 1', $point1->toString, 'and point 2', $point2->toString, 'is', $point1->distance($point2) ; # prints 'The Distance between point 1 (-7,- 4,3) and point 2 (17,6,2.5) is '

32 Other Atrribute types any Item Bool Maybe[`a] Undef Defined Value Str Num Int ClassName RoleName Ref ScalarRef[`a] ArrayRef[`a] HashRef[`a] CodeRef RegexpRef GlobRef FileHandle Object

33 package Farm; use Moose; has 'animals' => ( traits => ['Array'], is => 'ro', isa => 'ArrayRef[Animal]', default => sub { [] }, handles => { all_animals => 'elements', add_animal => 'push', find_animal => 'first', get_animal => 'get', count_animals => 'count', has_animals => 'count', has_no_animals => 'is_empty', sorted_animals => 'sort', }, ); no Moose; 1;

34 package Galaxy; use Moose; has 'planets' => ( traits => ['Hash'], is => 'ro', isa => 'HashRef[Planets]', default => sub { {} }, handles => { set_planets => 'set', get_planets => 'get', has_no_planets => 'is_empty', num_planets => 'count', delete_planets => 'delete', }, ); no Moose; 1;

35 Further Information Moose does a lot more We have only scratched the surface Good documentation  CPAN  Moose::Manual::*  Moose::Cookbook::* No good book yet