Programming in Perl references and objects Peter Verhás January 2002.

Slides:



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

COMP234 Perl Printing Special Quotes File Handling.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
Perl Functions Software Tools. Slide 2 Defining a Function l A user-defined function or subroutine is defined in Perl as follows: sub subname{ statement1;
Introduction to Perl. How to run perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Your program/script.
CSC3530 Software Technology Tutorial Two PERL Basics.
Introduction to a Programming Environment
I/O while ($line= ){ #remove new line char \n chomp($line); if($line eq “quit”){ exit(1); } while ( ){ #remove new line char \n chomp($_); if($_ eq “quit”){
Perl File I/O and Arrays. File I/O Perl allows to open a file to read, write, or append As well as pipe input or output to another program. —We get to.
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
2ex.1 Lists and Arrays. 2ex.2 Comments on exercises Always run your script with “ perl -w ” and take care of all warnings  submitted scripts should not.
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
CSE S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar.
1 - buttons Click “Step Forward” to execute one line of the program. Click “Reset” to start over. “Play,” “Stop,” and “Step Back” are disabled in this.
© iPerimeter Ltd Unix and IBM i  AIX and Linux run natively on Power Systems  IBM i can do Unix type things in two ways:  Posix/QShell  Ordinary.
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
Perl Refernces. Kinds of references: hard: a scalar variable that points to data symbolic: a variable that names another reference typeglob: a kind of.
Writing Perl Extensions: There’s more than one way to do it.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
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.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
Languages and the Machine Chapter 5 CS221. Topics The Compilation Process The Assembly Process Linking and Loading Macros We will skip –Case Study: Extensions.
Perl Practical(?)‏ Extraction and Report Language.
Topic 11: Object-oriented Perl CSE3395 Perl Programming Camel3 chapter 12, pages perlobj, perltoot, perlbot, perlmod manpages.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Introduction to Perl Yupu Liang cbio at MSKCC
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Programming in Java CSCI-2220 Object Oriented Programming.
Programming in Perl predefined variables Peter Verhás January 2002.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Perl Basics. sh-bang !!!! Every perl program starts with a sh-bang line #!/usr/bin/perl # hello.pl printf “Hello, world!\n”; printf STDOUT “Hello, world!\n”;
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
A Few More Functions. One more quoting operator qw// Takes a space separated sequence of words, and returns a list of single-quoted words. –no interpolation.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
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.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
 History  Ease of use  Portability  Standard  Security & Privacy  User support  Application &Popularity Today  Ten Most Popular Programming Languages.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Intro to Object Oriented Perl Packages, Modules, Classes.
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Object Oriented Programming Programming Perl Chapter 12: "Objects"
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Built-In Functions. Notations For each function, I will give its name and prototype. –prototype = number and type of arguments ARRAY means an actual named.
Input from STDIN STDIN, standard input, comes from the keyboard.
Perl Modules.
Programming in Perl Introduction
Subroutines Web Programming.
Perl Functions.
Lesson 2. Control structures File IO - reading and writing Subroutines
Programming in Perl Tutorial Introduction
Subroutines.
Presentation transcript:

Programming in Perl references and objects Peter Verhás January 2002.

References (pointers) $apple = 113; $pear = 'apple'; $plum = \$apple; print "$$pear = $$plum\n"; OUTPUT: 113 = 113 $pear is soft reference $plum is hard reference

Easy memory handling $apple = 114; sub pointir { my $apple = 113; return \$apple; } $pear = 'apple'; $plum = &pointir; print "$$pear = $$plum\n"; OUTPUT: 114 = 113 There is nothing like malloc() in C. Do not try to allocate memory, Perl will take care. Don’t try to release memory, Perl will take care. Pay attention to programming instead.

Only space is reserved not variable sub pointir { my $apple = 113; return \$apple; } $pear = &pointir; $plum = &pointir; $$plum++; print "$$pear < $$plum\n"; OUTPUT: 113 < 114 The local variable $apple is not kept, only the memory where the value is stored. $$plum and $$pear are two different variables

Complex Reference Variable Example $apple = sub { = (1,1,3); return }; $melon = sub { my %apple = (0,1,1,1,2,3); return \%apple; }; $pear = &$apple; $plum = &$melon; $$pear[2]++; $pear->[2]--; $$plum{2}++; $plum->{2}--; for $i (0.. 2){ print $$pear[$i] } print ' = '; for $i (0.. 2){ print $$plum{$i} } OUTPUT: 113 = 113 Variables can reference arrays, hash variables, functions and objects (see later).

Array of arrays = ( [ "fred", "barney" ], [ "george", "jane", "elroy" ], [ "homer", "marge", "bart" ], ); $ref_to_AoA = [ [ "fred", "barney" ], [ "george", "jane", "elroy" ], [ "homer", "marge", "bart" ], ]; print $AoA[2][2]; print $ref_to_AoA->[2][2]; print $AoA[2]->[2]; print $ref_to_AoA->[2]->[2]; You can omit -> from between ] and [

Array of arrays = ( { "fred„ => "barney" }, { "george", "jane" }, [ "homer", "marge", "bart" ], ); print $AoA[2][2]; print $AoA[0]{’fred’}; You can omit -> from between ] and [ between ] and { between } and [ between } and {

Symbol tables foo SCALAR ARRAY HASH CODE IO GLOB %foo sub foo { 1; } open(foo,”file.txt”) *foo{SCALAR} *foo{ARRAY} *foo{HASH} *foo{CODE} *foo{IO} *foo{GLOB} $main::

= (1,2,3,4,5,6,); $foo = 120; %foo = ( 1=>2, 2=>3, 'apple' =>'peach'); sub foo { 1; } open(fo,"test.pl") or die; print ${*foo{SCALAR}}; print "\n"; print ${*foo{ARRAY}}[1]; print "\n"; print ${*foo{HASH}}{'apple'}; print "\n"; print &{*foo{CODE}}; print "\n"; $fh = *fo{IO}; print scalar ; print ${*foo{GLOB}}; print "\n"; OUTPUT: peach = (1,2,3,4,5,6,); *main::foo

Practical use of typeglob *a = *b; –a becomes the same as b *PI = \ ; –Defines unalterable symbol

Modules Modules written by others –use CGI; and use it, don’t care how it is written (CGI is an example, there are a lot of modules) Write your own modules

Writing your own module { package fruit; sub worm { $apricot = 'apple'; 'carotene' } } $apricot = &fruit::worm; print "$apricot, $fruit::apricot\n";

Special subroutines in the module BEGIN –A BEGIN subroutine is executed as soon as possible. Unloaded after execution. CHECK –After compilation, before runtime reverse order INIT –After compilation, before runtime forward order END –An END subroutine is executed as late as possible

Special subroutines example BEGIN { print 1; } END {print 2; } BEGIN { print 3; } END { print 4; } INIT { print 5; } CHECK {print 6; } INIT { print 7; } CHECK {print 8; } OUTPUT: package main; is the default

Putting a module into a separate file package modul1; BEGIN { print "modul1 begin\n"; } END { print "modul1 end\n"; } sub hello { print „I love you!\n"; } 1; ’.’; use module1; holds all directories where the modules are looked for. BEGIN and END are called when the module is loaded and unloaded. TRUE value signaling successful load

Objects in Perl There is no „class” in Perl Object is a reference to something „blessed” into a class package myclass; sub new { bless {}; } Any reference can be blessed into any module (or in this case rather call it class).

Creating a new object Any function can create a new reference and bless it into a class. The creator should allocate the memory even though this is not a big deal in Perl. –bless {} actually creates the reference and blesses into actual class.

Class is a package Class Method is a sub in the module Inheritance through the package sub method { my $class = shift;.... } The first argument passed to a sub is the object.

Ways Calling Methods $object->method(args) method $object args –$object = new Class initargs; –print STDERR ”I warn you!” object->method need parentheses if there is argument. Always use -> notation!

Class Example { package baseclass; sub f0 { my $class = shift; my $arg = shift; print "baseclass f0 $arg\n"; } sub f1 { my $class = shift; my $arg = shift; print "baseclass f1 $arg\n"; } { package myclass; 'baseclass'; sub new { bless {}; } sub f1 { my $class = shift; my $arg = shift; print "myclass f1 $arg\n"; } sub f2 { my $class = shift; my $arg = shift; print "myclass f2 $arg\n"; } sub AUTOLOAD { my $class = shift; print "autoload myclass $AUTOLOAD\n"; } $q = new myclass; $q->f0(1); f1 $q 1,2,3 ; myclass::->f2(1); $q->f3(1); baseclass f0 1 myclass f1 1 myclass f2 1 autoload myclass myclass::f3 autoload myclass myclass::DESTROY OUTPUT:

Method DESTROY If this function is defined is automatically called when the object is not used any more. It gets only one argument, the object. This argument is read-only, but can be blessed into other class so that DESTROY for that class will be called.

Thank you for your kind attention and I hope that you learnt a lot from this tutorial and will help you learning Perl giving you a jump start.