Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…

Slides:



Advertisements
Similar presentations
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Advertisements

Advanced Perl WUC Perl Poetry Variable Scope.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Week 11 - Friday.  What did we talk about last time?  Object methods  Accessors  Mutators  Constructors  Defining classes.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
Introduction to Computers and Programming Introduction to Methods in Java.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
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.
1 Object Oriented Programming (OOP). Exam Friday Paper questions –6 Subroutines BLAT/BLAST Hash Modules Sorting 2.
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
Introduction to the Windows API n API - Application Programming Interface n an API is the software interface for things such as the OS n an API is the.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
CSC 142 D 1 CSC 142 Instance methods [Reading: chapter 4]
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen.
1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
CS Introduction to Data Structures Spring Term 2004 Franz Hiergeist.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Intro to Object Oriented Perl Packages, Modules, Classes.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
OOP Basics Classes & Methods (c) IDMS/SQL News
Programming in Perl references and objects Peter Verhás January 2002.
1 Using Perl Modules. 2 What are Perl modules?  Modules are collections of subroutines  Encapsulate code for a related set of processes  End in.pm.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
REEM ALMOTIRI Information Technology Department Majmaah University.
OOP: Encapsulation &Abstraction
Object-Oriented Programming: Classes and Objects
Haidong Xue Summer 2011, at GSU
Perl Modules.
Object-Oriented Programming: Classes and Objects
METHODS AND BEHAVIORS AKEEL AHMED.
Chapter 6 Methods: A Deeper Look
Week 6 Object-Oriented Programming (2): Polymorphism
Introduction to Classes and Objects
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
CISC/CMPE320 - Prof. McLeod
Tonga Institute of Higher Education
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Java IDE Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Object Oriented Programming
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…

Classes in Perl A class is defined by storing code which defines it in a separate file, and then use ing that file The file must be named with the name of the class (starting with an capital letter), followed by the extension.pm After the shebang in your ‘main’ file, this line of code: use ; You can then create instances of the class anywhere in your file.

Defining an Object In Perl, an object is simply a reference containing the members of a class. –typically, a reference to a hash, but can be any kind of reference The reference becomes an object when it is “blessed” – when you tell Perl the reference belongs to a certain class.

Simple Example package Student; $obj = {Name => ‘Paul’, ID => 123}; bless ($obj, Student); $obj is now an object of the class Student ‘package Student’ is the first line of your.pm file. It identifies all following code as belonging to this class/package/module

Constructors Unlike C++, a Constructor in Perl is simply another subroutine. Typically named ‘new’, but you can give it any name you want. package Student; sub new { my $ref = {Name => “”, ID => 0}; bless ($ref, Student); return $ref; } In this example, don’t actually have to give $ref any elements. You can define them all in a later subroutine, if you choose.

Calling the Constructor As you may be able to guess, TMTOWTDI $student = new Student; $student = Student->new; $student = Student::new(Student); First two methods get translated to 3 rd method internally by perl. This has beneficial consequences…

Arguments to Constructor (actually, this applies to arguments to any method) Every time the constructor is called, first argument to function is the name of the class. Remaining arguments are caller-defined $obj = new Student (“Paul”, 123); $obj = Student->new(“Paul”, 123); $obj = Student::new(Student, “Paul”, 123); So, when defining constructor, often see this: sub new{ my $class = shift; my ($name, $ID) my $ref = {Name => $name,ID => $ID}; bless ($ref, $class); return $ref; }

More Methods Within the.pm file, any subroutines you declare become methods of that class. For all methods, first argument is always the object that the method is being called on. This is also beneficial… sub setName{ my $ref = shift; my $name = shift; $ref->{Name} = $name; } To call this method: $obj->setName(“Paul Lalli”); Perl translates this to: Student::setName($obj, “Paul Lalli”);

One more thing… In one of the oddest things I’ve learned about Perl, you need to place the following statement at the end of your.pm file: 1; This is because the ‘use’ keyword needs to take something that returns a true value. Perl ‘returns’ the last statement evaluated.

 Be Kind… to One Another  Note that class variables are not strictly ‘private’ in the C++ sense. There is nothing preventing the user of your class from modifying the data members directly, bypassing your interface functions. Perl’s general philosophy is “If someone wants to shoot himself in the foot, who are you to stop him?” When using other people’s classes, almost always a better idea to use the functions they’ve given you, and pretend you can’t get at the internal data. There are, of course, methods you can use to prevent users from doing this. –Really not worth the trouble –Significantly beyond scope of this course

Standard Modules Perl distributions come with a significant number of pre-installed modules that you can use in your own programs. These files are found in system-specified directories. To find where the files are located on your system, examine array: print Gives a listing of all directories that are looked at when Perl finds a use statement.

Standard Module Example We’ll look at a pre-defined class that implements Complex-number support in Perl. This module is defined in a subdirectory of the include path, called Math. use Math::Complex; The constructor for this class is called make $z = Math::Complex->make(4,3); –Creates an instance of the class Complex. $z can be thought to hold the value 4 + 3i

More Math::Complex Complex.pm is also good enough to overload the basic mathematical operators to work with members of the complex class –Overloading operators is beyond the scope of this lecture – read the Camel chapter 13 for more info It also defines a constant i = sqrt(-1); use Math::Complex; $z = Math::Complex->make(3,5); print “$z\n”; $z = $z + 4; print “$z\n”; $z = $z – 3*i; print “$z\n”; Prints: 3 + 5i 7 + 5i 7 + 2i

Privacy? Bah… If you examine the Complex.pm file, you’ll see that the internal structure of the class is (at least partially) represented by an array reference called cartesian. Perl will let you modify this directly: $z = Math::Complex->make (3,5); ${$z->cartesian}[0] = 40; Don’t do that. Instead, use the functions provided by the class: Re() and Im() $z->$Re(40); #set real part of $z to 40 $img = $z->Im(); #get img. part of $z

Help on Standard Modules For documentation for all these classes, you have several resources: Camel, chapter 32 Unix program perldoc –ex, perldoc Math::Complex CPAN –