Advanced Perl WUC 2006. Perl Poetry Variable Scope.

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

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.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
9.1 Subroutines and sorting. 9.2 A subroutine is a user-defined function. Subroutine definition: sub SUB_NAME { STATEMENT1; STATEMENT2;... } Subroutine.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
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;
Object-Oriented Programming. Libraries If you have subroutines that you use repeatedly, you will probably be happier if they are kept in a single location.
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.
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
10.1 Sorting and Modules בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
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.
CSE S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar.
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
subroutines and references
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
Computer Programming for Biologists Class 8 Nov 28 th, 2014 Karsten Hokamp
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.
OOP with PHP Roman Bednarik
Inheritance in the Java programming language J. W. Rider.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
ILM Proprietary and Confidential -
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.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C++ Programming Part 2 Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
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.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
1 PERL Functions. 2 Functions Functions also called subroutines are “free flowing”. The returned value from a function can be interpreted in many different.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Intro to Object Oriented Perl Packages, Modules, Classes.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
Advanced Perl For Bioinformatics Part 1 2/23/06 1-4pm Module structure Module path Module export Object oriented programming Part 2 2/24/06 1-4pm Bioperl.
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.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
References and Data Structures
Object-Oriented Programming
Perl Modules.
Subroutines Web Programming.
Perl Functions.
Tonga Institute of Higher Education
References and Objects
OOP Aga private institute for computer science 5th grade
Subroutines.
INTRODUCTION to PERL PART 1.
C++ Object Oriented 1.
Presentation transcript:

Advanced Perl WUC 2006

Perl Poetry

Variable Scope

Global Variables By default, all variables are global Output:Hello! In general, don’t use global variables

Using “my” Limits scope to a block of code Output: 5 2 Lexically scoped –Not limited to a single code block

Using “local” Temporary copy of a global variable Necessary in certain situations, but as a general rule, not used. Output: $value = 2 $value = 1

use strict; Variables must be declared use strict; use strict ‘vars’; use strict ‘subs’; no strict; Distrust bare words In general, makes it harder to write bad code Output: First Program: test_value Second Program: test passed

References

Hard References Scalars that refer to other data Any type of data can be referred to, including other references Used primarily for efficiency

Creating References Backslash “\” is the reference operator  Scalar (\$) my $var = “WebGUI”; my $ref = \$var;  Array = (“WebGUI”,”7.0”); my $ref =  Hash (\%) my %hash = ( “WebGUI” => ”7.0” ); my $ref = \%arr;  Subroutine (\&) sub hello { print “Hello”; }; my $ref = \&hello;

Dereferencing Place the data type symbol in front of the reference.  Scalar ($$) my $var = $$ref; my $var = ${$ref};  Array my $val = $ref->[0];  Hash (%$) my %hash = %$ref; my %hash = %{$ref}; my $item = $ref->{“WebGUI”}  Subroutine (&$) &$ref; &{$ref}; $ref->(); Using the arrow operator

Anonymous References No need to create the data type  Array = (1,2,3,4,5); my $arr_ref = [1,2,3,4,5];  Hash my %hash = (1=>1,2=>2); my $href = {1=>1,2=>2};  Subroutine sub hello { print “Hello”; } my $hello = sub { print “Hello”; }; Almost exactly the same as creating the data type In most cases, it saves you a step

Data Structures Store multiple dimensions of data foreach my $arr_ref { foreach my $data { print $data; } foreach my $arr_ref { print $arr_ref->[0]; print $arr_ref->[1]; print $arr_ref->[2]; } Data Structures are combinations of anonymous array and hash references my $ref = [ [“Frank”,”Dillon”,” ”], [“JT”,”Smith”,” ”], [“Colin”,”Kuskie”,” ”] ]; print “First Name:”, $ref->[0]->[0]; print “ Last Name:”, $ref->[0]->[1]; print “ Phone:”, $ref->[0]->[2];

Advanced Data Structures Multiple data types Using Data::Dumper $ref = { a=>[ { last”=>”Allan”, first”=>”Richard”, phone”=>[“ ”,” ”] }, { last=>”Anderson”, first=>”Kevin”, phone=>{ mobile=>” ”, home=>” ” } }, ], b=>[ { last=>”Bach”, first=>”Randy”, phone=>[“ ”,” ”] }, { last=>”Bonds”, first=>”Kim”, phone=>{ mobile=>” ”, home=>” ” } }, ], }; Determining the reference type if ( ref $ref->{$i}->[$j]->{$k} eq “ARRAY” ) { print $ref->{$i}->[$j]->{$k}->[0]; } else { print $ref->{$i}->[$j]->{$k}->{0}; } use Data::Dumper; print Dumper($ref);

Reusable Code

Libraries

Define a Library Libraries are simply.pl files All libraries must return 1 (true)

Use the Library The shebang! The “require” statement./doit.pl

Packages

Creating a Package Packages define a namespace Define your package Return true

Using a Package The “use” statement “use” vs “require” Calling your method Setting package data

Objects

A Little Theory What are Objects? Objects vs Classes PIE –Polymorphism –Inheritance –Encapsulation

Creating Classes Create a Constructor –bless ( ref, classname) Add Data Members –Data Access Methods –Private vs Public Add Methods –Instance Methods –Class Methods Create a Destructor

Creating Objects Instantiating a Class Invoking a Method Instance Variables –Storing Data

Inheritance Understanding Overriding Methods SUPER Output: Ref: Class2 3+4=: 7 Output: Ref: Class2 3+4=: 17

Q&A