COMS 3101 Programming Languages: Perl. Lecture 5

Similar documents
COMS 3101 Programming Languages: Perl. Lecture 6

COMS 3101 Programming Languages: Perl. Lecture 2

PERL Scripting - Course Contents

CSCI-GA Scripting Languages

Data Structures (list, dictionary, tuples, sets, strings)

Perl Scripting. Students Will Learn. Course Description. Duration: 4 Days. Price: $2295

Chapter 5 Object-Oriented Programming

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Inheritance -- Introduction

Learning Perl Objects, References, and Modules

Software Design and Analysis for Engineers

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

package YourModule; require = = qw(munge frobnicate); # symbols to export on request

CMSC 132: Object-Oriented Programming II

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

CS 105 Perl: Perl subroutines and Disciplined Perl

G Programming Languages - Fall 2012

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Cpt S 122 Data Structures. Introduction to C++ Part II

CS 105 Perl: Modules and Objects

Object Oriented Design

Beginning Perl. Third Edition. Apress. JAMES LEE with SIMON COZENS

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Polymorphism Part 1 1

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Oops known as object-oriented programming language system is the main feature of C# which further support the major features of oops including:

Object-Oriented-Programming! (OOP)

CPS 506 Comparative Programming Languages. Programming Language

Table of Contents Preface Bare Necessities... 17

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

STRUCTURING OF PROGRAM

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

2559 : Introduction to Visual Basic.NET Programming with Microsoft.NET

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

C++ Inheritance and Encapsulation

COMS 3101 Programming Languages: Perl. Lecture 1

COMS 3101 Programming Languages: Perl. Lecture 3

@EXPORT_OK = qw(munge frobnicate); # symbols to export on request

Data Abstraction. Hwansoo Han

IN CHAPTER 7, SUBROUTINES AND MODULES, you learned how to organize


MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

PROGRAMMING LANGUAGE 2

use attributes (); # optional, to get subroutine declarations = attributes::get(\&foo);

CS111: PROGRAMMING LANGUAGE II

Object Oriented Programming and Perl

CMSC 132: Object-Oriented Programming II

Weeks 6&7: Procedures and Parameter Passing

Perl (5 Days Content)

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

1. Introduction. 2. Scalar Data

C++ Programming: Polymorphism

Java: introduction to object-oriented features

Perl for Biologists. Session 9 April 29, Subroutines and functions. Jaroslaw Pillardy

Interview Questions of C++

Subroutines. Subroutines. The Basics. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc.

CS105 C++ Lecture 7. More on Classes, Inheritance

COMP322 - Introduction to C++ Lecture 10 - Overloading Operators and Exceptions

Lecture 23: Object Lifetime and Garbage Collection

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Chapter 1: Object-Oriented Programming Using C++

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Object Oriented Programming with Perl

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

A Short Summary of Javali

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Programming for the Web with PHP

C++ Important Questions with Answers

Introduction to Programming Using Java (98-388)

CP150 - Advanced Perl Programming

Fast Introduction to Object Oriented Programming and C++

Chapter 6 Introduction to Defining Classes

CS304 Object Oriented Programming Final Term

CS313D: ADVANCED PROGRAMMING LANGUAGE

Building Multitier Programs with Classes

Programming, numerics and optimization

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

autodie - Replace functions with ones that succeed or die with lexical scope # Recommended: implies 'use autodie qw(:default)'

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

JAVA: A Primer. By: Amrita Rajagopal

PART A : MULTIPLE CHOICE Circle the letter of the best answer (1 mark each)

Advanced Programming Using Visual Basic 2008

CPSC 427: Object-Oriented Programming

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

What is Inheritance?

Procedure and Object- Oriented Abstraction

C# Programming in the.net Framework

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

The Eobj Perl environment

Introduction to Programming Microsoft.NET Applications with Visual Studio 2008 (C#)

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Chapter 2. Building Multitier Programs with Classes The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CGS 2405 Advanced Programming with C++ Course Justification

Java Programming Training for Experienced Programmers (5 Days)

Transcription:

COMS 3101 Programming Languages: Perl Lecture 5 Fall 2013 Instructor: Ilia Vovsha http://www.cs.columbia.edu/~vovsha/coms3101/perl

Lecture Outline Packages & Modules Concepts: Subroutine references Symbolic references Garbage collection Saving structures Objects and Classes Next: More OOP, CPAN 5.2

Remarks Pattern matching cage can be any character: m// or // is equivalent to m{} s/// is equivalent to s{} {} // are just customary quote characters for pattern matching behavior. In fact you could choose your own character instead of {} (e.g. m!!) Convenient if lots of slashes in the pattern ref function: returns type of reference (a string) $rtype = ref($href); # returns HASH $rtype = ref($aref); # returns ARRAY if ( ref($href) eq HASH ) { } 5.3

Packages & Modules Why do we need them? Package or module, what is the difference? use vs. require Importing from another package Pragmatic modules (pragmas) 5.4

Packages (purpose) sub parse_text { $count = $count++; } sub normalize { $count++; } # code from one file # code from another file # Use both functions: parse_text(); normalize(); print $count\n ; # Which $count? What is its value? 5.5

Packages (definition) Balanced code: abstraction + reuse Every chunk of code has its own namespace. In Perl, a namespace is called a package Independent of files: Multiple packages in one file Single package spanning multiple files Most common: one package per file Best approach: one package per file where file name is package name + extension.pm pm = perl module Each package has its own symbol table Default current package: main 5.6

Packages (example) package Parse; sub parse_text { $count = $count+1; } package Process; sub normalize { $count++; } # namespace: Parse # namespace: Process # code from another file package main; # Implicit if no package declaration Parse::parse_text(); Process::normalize(); print $Parse::count\n ; # count from Parse package print $count, $main::$count\n ; # global variable count 5.7

Modules (definition) Fundamental unit of code reusability in Perl Module: one package in one file where file name is package name + extension.pm Two types: Traditional: define vars/subs for caller to import / use Object oriented: specify class definitions, and accessed through method calls To include module in code: use or require Naming convention: upper case letters 5.8

Modules (example) # code.pl use Parse; use Process; Parse::parse_text(); Process::normalize(); print $Parse::count\n ; # Parse.pm package Parse; $count = 0; sub parse_text { $count = $count+1; } 1; # use must succeed! # Process.pm package Process; $count = 0; sub normalize { $count++; } 1; 5.9

Modules (use vs. require) To include module in code: use Preloadat at compile time: compiler loads module beforecompiling rest of file (can change behavior, visibility) Imports requested symbols (Exporter module): Specify explicitly Import symbols from @EXPORT and @EXPORT_OK OK Once imported, can be used without package name qualifier Access imported vars/subs with :: Process::normalize(); # With qualifier normalize(); # No qualifier Searching for modules: In each directory listed in @INC array Modify @INC at compile time with lib pragma require loads module at run time Actually, no compelling reason to prefer require over use 5.10

Importing from Package Modules export symbols by inheriting the import method from the Exporter module Symbols exported by default: @EXPORT Symbols exported by request: @EXPORT_OK # Parse.pm package Parse; use Exporter; our @ISA = ( Exporter ); # Inheritance our @EXPORT = qw($count &parse_text); our @EXPORT _ OK = qw(%h @arr); # code.pl use Parse; # use Parse qw(%h @arr) parse_text(); %Parse::h = (); sub parse_text { } 1; 5.11

Pragmas Pragmatic modules (pragmas) are hints to compiler Only work with use / no (seen at compile time) Convention: names in lowercase letters Lexically scoped (just like my variables), effects limited to enclosing block Invoke: use <pragma> Disable: no <pragma> 5.12

Pragmas (usage) Recall, to show warnings: #!/usr/local/bin/perl w Pragmas are preferable use warnings; g; # Enable warnings till end of file... { no warnings; # Disable warnings till end of block... } # warnings are enabled back 5.13

Pragmas (examples) warnings: Perl complains about variables that are used only once, variable ibl re declarations, improper conversions etc. use warnings; use warnings qw(io syntax); strict: Perl is strict about what is legal code (subs,vars,refs) vars: variable must be predeclared refs: can t use symbolic references subs: barewords are syntax errors (must quote strings) use strict; use strict vars ; # all three # variables only 5.14

Pragmas (more examples) constant: declare named symbol an immutable constant, requires separate declaration for each symbol use constant ARR_SIZE => 100; use constant ARR_REF => [1,2,3,4]; # Can t interpolate ARR_SIZE into string (no $ sign in front)! lib: add directories to default search path, at compile time use lib /myperl/code/ ; # Add to @INC no lib /yourperl/code/ ; # Delete from @INC # You should delete only directories you added 5.15

Concepts Subroutine references Copying referent Symbolic references Garbage collection Saving structures 5.16

Subroutine References Reference to a sub follows similar rules Can use backslash or anonymous sub max {. } # Anonymous $sub_ref = \&max; $sub_ref = sub {. }; # Calling subroutine &$sub_ref(); $sub_ref > (); $sub_ref > (1,4,6,8); # Call sub using string my %cmds = ( process => \&process_data, clear => \&clear_data, any => sub {.} ); $cmds{$str} > (); 5.17

Copy Referent To copy referent for another reference: $aref1 = [ 1,2,3 ]; $aref2 = [ 4,5,6 ]; $aref2 = $aref1 $aref2 = [ @$aref1 ]; $href2 = { %{$href1 } }; # Not a copy! Rather refers to the same location! # Create a new anonymous reference # Same approach for hashes 5.18

Symbolic References Refers to the name of a variable If we try to dereference, we get access to variable! Can be dangerous! (use strict refs to prohibit) $N = V ; $$N = 5; # Set scalar $V = 5 $N > [0] = 6; # Set element 0 of array V to 6 $N > {key} = 7; # Set key of hash V to 7 &$N; # Call sub V 5.19

Garbage Collection High level language: don t worry about de allocating memory Garbage collection = automatic reclamation process Block exited: locally scoped variables are freed up Don t hide your garbage! (circular references) Using multiple large hashes in the same scope? Loop through the keys and delete each one For objects: use destroy methods 5.20

Saving Data Structures Save any DS to use later: Data::Dumper module Turn DS into a string, save externally to a file, read in the file and recover DS with eval / do CPAN module Storable: more efficient, but cannot be shared across different architectures use Data::Dumper; open (OUTFILE, > filename ); print OUTFILE Data::Dumper >Dump([\%hash], [ *hash ]); open (INFILE, < filename ); undef $/; # undef record separator, read entire file eval <INFILE>; # recreate %hash use Storable; store(\%hash, filename ); $href = retrieve( filename ); %hash = % { retrieve( filename ) } ; 5.21

OOP Review of concepts: objects, classes, methods Object Oriented Programming in Perl Method invocation Object construction: constructor bless function initialization destructor Inheritance 5.22

OOP (concepts) Program: collection of objects Object: dt data structure t with collection of behaviors bh Object: instance of a class Class: defines attributes (variables) and methods (functions), i.e. behavior applied to class/instances Each instance of class (object) keeps track of its own attribute values Instance methods refer to actions of specific object Class methods refer to actions of entire class of (many) objects To generate a new object we use a constructor method 5.23

OOP (more concepts) Can share methods between classes: inheritance Derived / sub class inherits methods from base / parent / super class The derived class can update the behavior of the parent class Given an (derived) object, how do we select the most appropriate method for it? Polymorphism General design principle: when using the object, the object is a black box (we shouldn t manipulate attributes / methods directly) Encapsulation: access objects through methods alone 5.24

OOP in Perl Perl supports OOP, but does not enforce the rules : you can break encapsulation Not the best choice for extensive OOP projects (not native). OOP is slower than a non OOP solution Can often write good code without using all OOP techniques Supports OOP techniques: single/multiple inheritance, method overriding, destructors, operator overloading No special syntax for objects: Objectsare references / referents Classes are packages (usually modules) Methods are subroutines 5.25

OOP (method invocation) To access an object indirectly, invoke method Invocation: locate sub (method) determined by class of invocant and method name Call the sub, passing invocant as its first argument Explicit invocation: using arrows. INVOCANT >METHOD() Invocant can be package name or reference (to object) package name: 1 st argument to class method is class name (string) reference: 1 st argument to instance method is reference (to object) $mazda = Car > new(); $mazda > drive( slow ); $method = new ; $mazda = Car > $method(); # Class: Car, Method: new, Object: $mazda # Instance: $mazda, Method: drive # Don t know method name ahead of time 5.26

OOP (constructor) Note: method s package is resolved at run time (don t know invocant) Note: regular sub s package is resolved at compile time Constructor is just a subroutine.err method (typically invoked by the package name) that returns an object reference # Car.pm package Car; sub new { my $invocant = shift; my $self = { }; bless ($self, $invocant); return $self; } # Constructor # 1 st argument is always invocant # Reference to empty hash # Return reference to object 5.27

OOP (bless function) To work on an object, reference must be marked with its class name The act of marking (turning ref into object ref) is called blessing bless function: 1 st argument is ref, 2 nd argument is class name to bless into (default: current package) # Car.pm package Car; sub new { # Constructor (hides the bless) my $invocant = shift; my $self = { }; bless ($self, $invocant); # bless (REF, Car ); return $self; } 5.28

OOP (initialization) Typical approach: use reference to an anonymous hash (but could be any type of reference) Hash can be non empty: maintain internal information (attributes) which are only manipulated by the object s methods bless function: 1 st argument is ref, 2 nd argument is class name to bless into (default: current package) # Car.pm # Call: $lexus = Car > new(maker=> lexus ); package Car; sub new { my $class = shift; sub new { my $self = { my $class = shift; maker => mazda, my $self = { @_ }; color => black, bless ($self, $class); @_, return $self; }; }. } 5.29

OOP (destructor) Destructors are rarely needed in Perl (automatic memory management) Dfi Define a subroutine DESTROY (required name, unlike new) Explicitly calling DESTROY is possible but seldom needed # Car.pm package Car; sub new {. # Constructor} sub DESTROY { # Destructor my $self = shift; # Attend to filehandles, databse connections } 5.30

OOP (inheritance) We can define a hierarchy of classes to share methods between them Derived d/ sub class inherits i methods from base / parent / super class The super classes are specified in the @ISA array (declared with our) of the derived class. Each element of the array is a package (class) name Single Inheritance: one parent class (search parent for methods) Multiple Inheritance: multiple parent classes (search left to right through @ISA array, depth first order) Once method is found, store in cache for efficiency If @ISA is changed, the cache is invalidated (avoid it if you can!) Constructor Inheritance: no automatic call to base class constructor 5.31

OOP (method search) If method not defined in class: search @ISA at runtime when a call is made Search order given multiple l parent classes: left to right lftt ihtthrough h @ISA array, depth first # Nissan.pm package Nissan; our @ISA = (Car, Truck); # Call: Nissan >new() >build( sedan ); # Search order: 1. Check if method (build) is defined in class (Nissan) 2. Check if method is defined in $Nissan::ISA[0] (Car class) 3. Check if method is defined in @Car::ISA (Car s @ISA array) 4. When done with ISA[0], repeat steps 2,3, for ISA[1] (Truck class) 5.32

Scoped Variables my creates private variable visible only within block hidden from outside of enclosing scope, and hides previously declared variables with identical name confines name & value to scope suitable for scalar/array/hash variables our confines name only to scope (no effect on visibility) suitable for scalar/array/hash variables used to access global variables, their initial value inside block unchanged effects or assignmentpersist after the scope of declaration (block) local confines value only to scope suitable for scalar/array/hash + more variables initial value for variable is () or undef value of variable is restored no matter how you exit the block (changes thrown away) dynamic scope: value of variable depends on scope & changes during run time my is preferable over local APP

Scoped Variables (example) $office = "global"; &say(); &barney(); &fred(); &say(); sub say { print "$office\n"; } # Global $office # prints "global" # prints "barney global", lexical scope; # prints "fred fred", dynamic scope, # prints "global", restored after &fred() # print the $office sub barney { my $office = "barney"; print "$office "; &say(); } sub fred { local $office = "fred"; print "$office "; &say(); } APP