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
Advanced Perl WUC Perl Poetry Variable Scope.
Advertisements

An introduction to pointers in c
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
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.
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
References and Data Structures. References Just as in C, you can create a variable that is a reference (or pointer) to another variable. That is, it contains.
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.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
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.
CS Winter 2011 Introduction to the C programming language.
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
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.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
Introduction to Methods
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.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
subroutines and references
4.1 Instance Variables, Constructors, and Methods.
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.
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.
Control Structures. Important Semantic Difference In all of these loops we are going to discuss, the braces are ALWAYS REQUIRED. Even if your loop/block.
Functions Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad.
Local Variables Garbage collection. © 2006 Pearson EducationParameters2 of 10 Using Accessors and Mutators Together What if you want to get a property.
– Intermediate Perl 10/23/ Intermediate Perl - References Intermediate Perl Session 2 · references, continued · function.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
References. Analagous (somewhat) to pointers in C/C++ –Far less messy, and definitely less dangerous –Variables that "refer" to another variable You can.
Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen.
1 CS161 Introduction to Computer Science Topic #9.
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.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
1 PERL Functions. 2 Functions Functions also called subroutines are “free flowing”. The returned value from a function can be interpreted in many different.
Intro to Object Oriented Perl Packages, Modules, Classes.
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
OOP Basics Classes & Methods (c) IDMS/SQL News
Perl References arrays and hashes can only contain scalars (numbers and strings)‏ if we want something more complicated (like an array of arrays) we use.
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method.
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 1 Day 6: References suggested reading: perlreftut
Andrew(amwallis) Classes!
Introduction to Perl: Part II
Intermediate Perl - References
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Lesson 2: Building Blocks of Programming
Java Programming Language
Object Oriented Programming
Subroutines.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
SPL – PS3 C++ Classes.
Threads and concurrency / Safety
Presentation transcript:

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

Before we can begin… Let’s talk about references We’ve used them a few times before –2D arrays, parameters to subroutines Never made anything formal Now we will, because without them, Perl objects can’t exist

References A reference is a pointer to a ‘real’ variable. We’ve seen how to create references to existing variables: –$array_ref = –$hash_ref = \%hash; –$scalar_ref = \$scalar Can also create Anonymous references…

Anonymous References These are pointers to values stored in memory that aren’t yet (or ever) labeled by a variable name. Syntax is similar (but different) to creating actual variable Array: –$array_ref = [1, 2, 3, 4, 5]; Hash: –$hash_ref = {Name => ‘Paul’, ID => 123}; Scalar: –$str_ref = \“A String\n”; –$int_ref = \456;

Dereferencing References We’ve seen before how to dereference entire arrays or hashes (and scalars): –%hash = %$hash_ref; –$scalar = $$scalar_ref; Depending on your point of view, dereferencing members of array- or hash- references is either easier or more complicated…

TMTOWTDI In fact, there are three… $ $a_ref [2] = “Hello”; ${$a_ref} [2] = “Hello”; $a_ref->[2] = “Hello”; $ $h_ref {$key} = $value; ${$h_ref} {$key} = $value; $h_ref->{$key} = $value; These are all valid and acceptable. Form you choose is whatever looks the best to you.

Okay, Let’s Get Started This will be an introduction only. We will cover the very basics. Anything more complicated goes beyond the scope of this course –In fact, even this introduction is treading pretty close to the edge of that scope.

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 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. –Significantly beyond scope of this course –Really not worth the trouble