CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Object Oriented Programming Programming Perl Chapter 12: "Objects"

Slides:



Advertisements
Similar presentations
15. Python - Modules A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand.
Advertisements

Chapter Modules CSC1310 Fall Modules Modules Modules are the highest level program organization unit, usually correspond to source files and.
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
Language of the Month If it’s December, it must be Ruby! Adam Coffman and Brent Beer.
Q and A for Chapter 3.4 – 3.14 CS 104 Victor Norman.
Types in Scripting Languages CS 351 – Programming Paradigms.
Alice Variables Pepper. Set to Java look Edit / preferences restart.
Ruby The Gem of new programming languages. An interpreted scripting language.
Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
 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.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Guide to Programming with Python Chapter Nine Working with/Creating Modules.
1 Spidering the Web in Python CSC 161: The Art of Programming Prof. Henry Kautz 11/23/2009.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.
11 Getting Started with C# Chapter Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a.
Levels of Architecture & Language CHAPTER 1 © copyright Bobby Hoggard / material may not be redistributed without permission.
Introduction to Python 2 Dr. Bernard Chen University of Central Arkansas PyArkansas 2011.
Python: Classes By Matt Wufsus. Scopes and Namespaces A namespace is a mapping from names to objects. ◦Examples: the set of built-in names, such as the.
Python Modules An Introduction. Introduction A module is a file containing Python definitions and statements. The file name is the module name with the.
H3D API Training  Part 3.1: Python – Quick overview.
Modules and Decomposition UW CSE 190p Summer 2012 download examples from the calendar.
(Chapter 10 continued) Our examples feature MySQL as the database engine. It's open source and free. It's fully featured. And it's platform independent.
CIT 590 Intro to Programming Lecture 4. Agenda Doubts from HW1 and HW2 Main function Break, quit, exit Function argument names, scope What is modularity!
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Also “open class” and other languages….  A module is a named group of methods, constants, and class variables  All classes are modules  Modules are.
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 1 Day 3: Collections suggested reading: Learning Perl (4th Ed.), Chapter 3:
Intro to Python Adriane Huber Debbie Bartlett Python Lab #1Python Lab #1 1.
Introduction to Perl Yupu Liang cbio at MSKCC
Review Please hand in your practicals and homework Regular Expressions with grep.
How to write & use Perl Modules. What is a Module? A separate Namespace in a separate file with related functions/variables.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
5 1 Data Files CGI/Perl Programming By Diane Zak.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
ProgLan Python Session 4. Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable,
Ruby Objects, Classes and Variables CS 480/680 – Comparative Languages.
Files Tutor: You will need ….
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.
Python Let’s get started!.
Python  Monty or Snake?. Monty?  Spam, spam, spam and eggs  Dead parrots  Eric Idle, John Cleese, Michael Palin, etc.
Chapter 15. Modules Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Intro to Object Oriented Perl Packages, Modules, Classes.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Modules. Modules Modules are the highest level program organization unit, usually correspond to source files and serve as libraries of tools. Each file.
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.
Introducing Python 3 Introduction to Python. Introduction to Python L1 Introducing Python 3 Learning Objectives Know what Python is and some of the applications.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
– Hello Ruby 6/26/ Hello Ruby Hello Ruby A scrumptiously delicious intro · Why Ruby? · What’s it look like? · So how.
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 1 Day 6: References suggested reading: perlreftut
Introducing Python Introduction to Python.
G. Pullaiah College of Engineering and Technology
Scope History of Ruby. Where can you use Ruby? General Features.
Python Let’s get started!.
Introduction to Python
Pamela Moore & Zenia Bahorski
Hashing Exercises.
Perl Modules.
Henning Schulzrinne Advanced Programming
1 Python Lab #1 Intro to Python Adriane Huber Debbie Bartlett.
To Get Started Paper sheet
Python Classes By Craig Pennell.
Lecturer: Mukhtar Mohamed Ali “Hakaale”
File IO and Strings CIS 40 – Introduction to Programming in Python
Alice Variables Pepper.
Lesson 2. Control structures File IO - reading and writing Subroutines
Object Oriented Programming in Python
Coding Concepts (Basics)
Review We've seen that a module is a file that can contain classes as well as its own variables. We've seen that you need to import it to access the code,
Presentation transcript:

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Object Oriented Programming Programming Perl Chapter 12: "Objects"

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Tuesday's Homework (Processing addresses) Two style suggestions

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Store mixed data in a hash my $record = [$addr,$state,$zip]; $record[1] # Less clear my $record = { 'address' => $addr, 'state' => $state, 'zip' => $zip, $record{'zip'} # More clear

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Memory is cheap Make multiple copies of data if it's easier to work with my %name_index; my %state_city_index; Counterpoint: multiple copies makes it easier to diverge

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Store parsed data, not raw strings $name_index{$name} = $line; – Harder to use later $name_index{$name} = { 'state' => $state, 'city' => $city, } – Easier to use later

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Wednesday's Homework

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Interesting problems

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Object Oriented Programming

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy A simple module: use FindBin; print "I was run from $FindBin::Bin\n"; There is a module called "FindBin", and it contains a variable $Bin.

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy A quick trick My module is next to my script – (ex path/main.pl) %./main.pl Hello, world! % cd.. % path/main.pl Can't locate Alan/Hello.pm

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Why? My directory isn't in the search path Can set the environment variable PERL5LIB Easier: use FindBin; use lib $FindBin::Bin; " use lib 'path' " lets you add to Perl's search path FindBin returns the the directory your script is in

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Another module: use Digest::MD5; my $encoded = Digest::MD5::md5_hex($password); Digest::MD5 provides the function md5_hex

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Packages Obviously Perl provides some sort of namespaces (C++) or packages (Java).

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy package package Alan::MyPackageName; Everything from that point forward is in the Alan::MyPackagename package. Stop at end of file, or the next package line. You start in a package cleverly called main.

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy package A single file can hold multiple packages – (ex. two-in-one.pl) A::hi(); B::hi(); package A; sub hi { print "Hello from A\n"; } package B; sub hi { print "Hello from B\n"; }

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy package per file/module Put a package in a file with of the same name: – (ex hello_world.pl, Alan/Hello.pm) In the file "Alan/Hello.pm": package Alan::Hello; sub hi { print "Hello, world!"; } 1; In "hello_world.pl": use Alan::Hello; Alan::Hello::hi();

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy That's all nice, but where at the objects?

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy A module implementing an object use FileHandle; my $fh = new FileHandle; $fh->open("</etc/services"); my $line = $fh->getline; $fh->close;

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Perl's Do-It-Yourself Object System package module blessing

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy bless my reference An object is a reference that is "blessed" into a package Usually the package is in a file/module of the same name

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy An example (ex. oo/before and oo/after)

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Common (simplified) usage sub new { my($class, $name) my($self) = { 'name' => $name, 'count' => 0, }, $class; bless $self, $class; return $self; }

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Common (simplified) usage sub new { my($class, $name) return bless { 'name' => $name, 'count' => 0, }, $class; }

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Create your own idiom? "new" isn't special You could call it "create" or "fred". new is a good name. Use that.

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Alternate calls alan->new(); new alan; $existing_instance->new();

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy $existing_instance->new(); Requires a tweak to support... sub new { my($class, $name) return bless { 'name' => $name, 'count' => 0, }, (ref($class) || $class); }

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Python class hello(object): def __init__(self, name): self._count = 0 self._name = name def hi(self): self._count += 1 print "Hello, %s (I've said hello to %s %d times)" % \ (self._name, self._name, self._count) hello_alan = hello("Alan") hello_alan.hi()

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Ruby class Hello def = = 0 end def + 1 puts "Hello, (I've said hello to times)" end hello_alan = Hello.new("Alan") hello_alan.hi

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Javascript Function as class function hello(name) { this.name = name; this.count = 0; this.hi = function() { this.count++; document.write("Hello, " + this.name + " (I've said hello to " + this.name + " " + this.count + " times)\n"); } hello_alan = new hello("Alan"); hello_alan.hi();

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Advanced Topics

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Inheritance package = ("Car"); A HondaCivic "is a" Car. If HondaCivic fails to implement a method, try car – (ex. inherit/*)

CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Freeing object memory General rule: don't worry about it More specific rule: ensure no variables point to an unwanted object, it will go away eventually – my $obj = new MyObject; – $obj = undef; Most specific rule: Break circular references – Many scripting languages only reference count and don't fully garbage collect