Beginning Moose Houston Perl Mongers, April 10 th 2014.

Slides:



Advertisements
Similar presentations
Flowcharts for C programmers Ian McCrum. flowcharts Diagrams are used in many places; they can be used as a design aid or a documentation aid. As a design.
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
Introduction to Object Oriented Perl Programming Issac Goldstand Mirimar Networks
Annoucements  Next labs 9 and 10 are paired for everyone. So don’t miss the lab.  There is a review session for the quiz on Monday, November 4, at 8:00.
Getting Organised Subroutines, modules and the wonder of CPAN.
OO Systems and Roles Curtis "Ovid" Poe
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
Advanced Programming Handout 9 Qualified Types (SOE Chapter 12)
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Perl Lecture #1 Scripting Languages Fall Perl Practical Extraction and Report Language -created by Larry Wall -- mid – 1980’s –needed a quick language.
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
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.
Esri Developer Summit 2012 Should You Use a Swiss Army Knife or a Simple Screwdriver?
Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Survey of Advanced Perl Topics Database access "OpSys"-like functions Signal Handling Inside-Out Objects.
Test 2 Part a You have 20 minutes Don’t forget to put your name on the test Closed book No computers Do your own work.
GENERAL CONCEPTS OF OOPS INTRODUCTION With rapidly changing world and highly competitive and versatile nature of industry, the operations are becoming.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Abstract Factory Pattern. What Is an Abstract Factory Pattern?  The Abstract Factory pattern is a creative way to expand on the basic Factory pattern.
EXAM 1 REVIEW. days until the AP Computer Science test.
Templates, Databases and Frameworks. Databases: DBI Common database interface for perl Provides a functional,
Belly to Belly Create Relationships to Create Referrals.
How to write & use Perl Modules. What is a Module? A separate Namespace in a separate file with related functions/variables.
Moose: A Postmodern Object System for Perl Stuart Skelton.
11 Introduction to Object Oriented Programming (Continued) Cats.
Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,
COPYRIGHT 2010: Dr. David Scanlan, CSUS OBJECTIVES: How to write classes How to create an object from a class using the "New" keyword. How to communicate.
Operator Overloading Week 5.
OOP with Objective-C Categories, Protocols and Declared Properties.
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.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
1 Reverse a String iPhone/iPad, iOS Development Tutorial.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
Learning OOP in PHP. What is OOP? OOP stands for Object Oriented Programming. OOP is a programming paradigm wherein you create “objects” to work with.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Inheritance and Virtual Functions Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class?
Advanced Perl For Bioinformatics Part 1 2/23/06 1-4pm Module structure Module path Module export Object oriented programming Part 2 2/24/06 1-4pm Bioperl.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Programming in Perl references and objects Peter Verhás January 2002.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
Addition and Subtraction With Eddy the Calculator! By: Paris Andrew.
Moose A postmodern metaclass-based object system for Perl 5.
Object Oriented Programming with Perl and Moose
Functions CIS 40 – Introduction to Programming in Python
Perl Modules.
Week 6 Object-Oriented Programming (2): Polymorphism
Review and Q/A.
Variables Title slide variables.
Header files.
Classes.
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
slides created by Ethan Apter and Marty Stepp
Object-Oriented Design AND CLASS PROPERTIES
Introduction to Object-Oriented Programming (OOP) II
Object-Oriented Design AND CLASS PROPERTIES
Object-Oriented Programming and class Design
Object-Oriented Design AND CLASS PROPERTIES
Presentation transcript:

Beginning Moose Houston Perl Mongers, April 10 th 2014

What is Moose? Moose is a complete Object Oriented system for Perl Moose allows the developer to define your class declaratively. It allows for objects to have attributes, roles, methods, types and more.

Why Moose? Moose is easy to use and allows the developer to focus more on what the object should be doing and not how to implement it. Moose does not require the knowledge blessing hashrefs and accessor methods. It also provides introspection for classes in order to learn about attributes, methods etc.

Creating an Object Attribute package Human; use Moose; has 'name' => ( is => 'rw', isa => 'Str', required => 1, predicate => ‘has_name’ ); 1; Attributes are declared with the has keyword

No More of this! Yay! package Thing; sub new { my $class = shift; return bless {}, $class; }

About Moose Attribute Properties An attribute is a property of a class that defines it. It should have a name and can have many other properties. Potential Moose attributes can include: Is Isa Required Predicate

Other Attribute Properties Clearer Builder Lazy Sub type Delegation Coercion

Example of Attribute clearer and builder has 'weight' => ( is => 'rw', isa => 'Int', builder => '_build_weight', clearer => 'clear_weight', ); sub _build_weight { return 155; } 1; Within our Human module from previous example.

Example of Sub Type use Moose; use Moose::Util::TypeConstraints;...code… subtype 'PositiveInt', as 'Int’; Don’t forget to use Moose::Util::TypeConstriants !

Using Our Moose Object #! /usr/bin/perl use Human; my $human = Human->new(name => 'Dan The Man Culver'); print "The human's name is ".$human->name."\n"; print $human->has_name."\n"; print $human->weight."\n"; $human->clear_weight; print $human->weight."weight is now cleared\n"; Because we made the name property required in our Human module, it is required to define the name when we call new Our predicate property ‘has_name’ for our module will return a true or false value

Extending our Moose Object package Programmer; use Moose; use DateTime; extends 'Human'; has 'coffee' => ( is => 'rw', isa => 'Str', lazy => 1, ); has 'money' => (# Continued next slide is => 'rw', isa => PostiveInt, ); Extend an object with extends ‘ ’

Extending our Moose Object (Cont.) has 'celebration' => ( is => 'rw', isa => 'DateTime', handles => { 'last_made_it_rain' => 'date' } ); sub celebrate { my $self = shift; my $dollars = shift; if ( $self->money ne $dollars ) { return 0; } $self->celebration( DateTime->now() ); return 1; } 1; Here in celebration attribute, handles is an example of delegation in Moose

Using our new Object Extension #! /usr/bin/perl use Programmer; my $programmer = Programmer->new( name => 'Dan The Man Culver', coffee => 'black', money => 1000, ); $programmer->celebrate($programmer->money); print $programmer->name." made it rain on ". $programmer->last_made_it_rain. " with ". $programmer->money." dollars while drinking ". $programmer->coffee." coffee.";

Using Moose Roles #! /usr/bin/perl package Coding; use Moose::Role; has 'is_coding' => ( is => 'rw', isa => 'Bool', ); sub code { my $self = shift; print "I'm trying to code..\n"; $self->is_coding(1); } 1; It helps to think of roles as something that can be done, or ask yourself it an ‘ing’ or ‘able’ can be added to the end of the desired role. In this example, a programmer can be coding

Using Moose Roles ( Cont. ) package Programmer; use Moose; use DateTime; extends 'Human'; with 'Coding'; … To use a role, simply add with ‘ ’ in the module that you intend on using the role with.

Using our Moose Role #! /usr/bin/perl use Programmer; my $programmer = Programmer->new( name => 'Dan The Man Culver', coffee => 'black', money => 1000, ); $programmer->code; I’m trying to code.. ( Output )

Recommended Resources CPAN Moose Newsletter/Mailing List

Any Questions?