References and Objects

Slides:



Advertisements
Similar presentations
Advanced Perl WUC Perl Poetry Variable Scope.
Advertisements

Lecture 6 More advanced Perl…. Substitute Like s/// function in vi: #cut with EcoRI and chew back $linker = “GGCCAATTGGAAT”; $linker =~ s/CAATTG/CG/g;
Welcome to lecture 5: Object – Oriented Programming in Perl IGERT – Sponsored Bioinformatics Workshop Series Michael Janis and Max Kopelevich, Ph.D. Dept.
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
Computer Programming for Biologists Class 9 Dec 4 th, 2014 Karsten Hokamp
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.
Dynamic Programming and Perl Arrays Ellen Walker Bioinformatics Hiram College.
CS 330 Programming Languages 10 / 14 / 2008 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;
11ex.1 Modules and BioPerl. 11ex.2 sub reverseComplement { my ($seq) $seq =~ tr/ACGT/TGCA/; $seq = reverse $seq; return $seq; } my $revSeq = reverseComplement("GCAGTG");
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.
Sup.1 Supplemental Material (NOT part of the material for the exam)
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
10.1 Sorting and Modules בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
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”){
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
The GUS 3.0 Perl Object Layer CBIL Jonathan Schug June
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)
Introduction to Perl & BioPerl Dr G. P. S. Raghava Bioinformatics Centre Bioinformatics Centre IMTECH, Chandigarh Web:
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.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
– Intermediate Perl 10/23/ Intermediate Perl - References Intermediate Perl Session 2 · references, continued · function.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes.
Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
Introduction to Perl Part III By: Bridget Thomson McInnes 6 Feburary 2004.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
How to write & use Perl Modules. What is a Module? A separate Namespace in a separate file with related functions/variables.
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.
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.
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.
Basic Variables & Operators Web Programming1. Review: Perl Basics Syntax ► Comments: start with # (ignored by Perl) ► Statements: ends with ; (performed.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
1 PERL Functions. 2 Functions Functions also called subroutines are “free flowing”. The returned value from a function can be interpreted in many different.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Object Orientated Programming in Perl Simulated Models of Termites.
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.
Programming in Perl references and objects Peter Verhás January 2002.
(Perl5) Data-structures Mark Overmeer MARKOV Solutions FOSDEM, 6 Feb 2011.
Perl Backticks Hashes printf Variables Pragmas. Backticks.
References and Data Structures
Introduction to Perl: Part II
Modules and BioPerl.
Object-Oriented Programming
Intermediate Perl - References
DBW - PHP DBW2017.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Perl Modules.
Perl: Functions, References, Etc.
Advanced Topics Web Programming.
Perl Variables: Array Web Programming.
Subroutines Web Programming.
LING/C SC/PSYC 438/538 Lecture 6 Sandiway Fong.
Perl Variables: Hash Web Programming.
Basic Input/Output Web Programming.
Perl Functions.
Lesson 2. Control structures File IO - reading and writing Subroutines
More Variables and Data Types: references/matricies
Subroutines.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Presentation transcript:

References and Objects

1 2 3 @a 1 2 3 1 2 3 @b @_

1 2 3 @a 1 2 3 a 1 2 3 @b @_ b

Arrays as arguments my @a = (2,4,6); my @b = (1,3,5); my $dot = &dotProduct(\@a,\@b); print "$dot\n";

\@array the backslash in front of a variable gives you a reference to that variable

%someHashTable = ( "name"=>"Harm" ); $ref2HashTable = \%someHashTable;

print $someHashTable{"name"},"\n"; print $ref2HashTable->{"name"},"\n"; print ${$ref2HashTable}{"name"},"\n"; $ref2HashTable->{"name"}="Harm";

From reference back to hash $reference->{key} ${$reference}{key} $$reference{key} keys(%{$reference})

nameless hashtable %someHashTable = ( "name"=>"Harm" ); #named hash table $refToHashTable = { }; #anonymous hash table

nameless array @someArray = (0,1,2,3,4,5); #named array $ref2Array = [0,1,2,3,4,5]; #anonymous array $ref2Array->[0]; ${ref2Array}[0]; scalar @{$ref2Array};

use Data::Dumper; my $ref2HT = {"name"=>"Harm"}; print ref($ref2HT),"\n"; print Data::Dumper->Dump([$ref2HT]); Output: HASH $VAR1 = { 'name' => 'Harm' };

sub a { print "I am A\n"; } sub b { print "I am B\n"; } sub run { $function = shift; print "Output of the function: "; &{$function}(); } run(\&b); Output: Output of the function: I am B

Hash tables, arrays, subroutines etc. can have references. References are single values and always take up the same space. That makes them very useful for passing complex data structures in and out of functions and storing in lists or hash tables.

Object oriented programming my $baby; wakeUp($baby); dress($baby); feed($baby); my $adult; $adult->wakeUp(); $adult->getDressed(); $adult->eat()

Object oriented programming my $sequence; makeAS($sequence); getGC($sequence); shuffle($sequence); my $sequence; $sequence->makeAS(); $sequence->getGC(); $sequence->shuffle()

"Nobody knows how to make a pencil "Nobody knows how to make a pencil. There's not a single person in the world who actually knows how to make a pencil." "In order to make a pencil, you have to get wood for the barrel. In order to get wood, you have to have logging. You have to have somebody who can manufacture saws. No single person knows how to do all that." Milton Friedman

Objects have properties: attributes DNA sequence, ID, features have skills: methods getGCcontent(), shuffle()

package Bio::Seq; sub new { my ($package,$sequence) = @_; my $self = {}; bless $self,$package; $self->{"seq"}=$sequence; return $self; } sub seq{ my $self = shift; #ref to a hash table return $self->{"seq"}; 1;

use Data::Dumper; use Bio::Seq; my $sequence = Bio::Seq->new("atgcag"); print $sequence->seq(),"\n"; print "\n"; print ref($sequence),"\n"; print Data::Dumper->Dump([$sequence],["Seq"]);

Output: atgcag Bio::Seq $Seq = bless( { 'seq' => 'atgcag' }, 'Bio::Seq' );

use Bio::Seq; #file: Bio/Seq use Bio::Seq; #file: Bio/Seq.pm my $seq = Bio::Seq->new("atgcag"); Same: my $seq = Bio::Seq::new("Bio::Seq","atgcag");

package Bio::SeqColl; #file: Bio/SeqColl package Bio::SeqColl; #file: Bio/SeqColl.pm sub new { my ($package,@sequences) = @_; my $self = {}; bless $self,$package; foreach $seq (@sequences) { if (ref($seq) ne "Bio::Seq") { return; } $self->{sequences}=\@sequences; return $self; 1;

use Data::Dumper; use Bio::Seq; use Bio::SeqColl; my $seq1 = Bio::Seq->new("atgcag"); my $seq2 = Bio::Seq->new("tatatataaa"); my $seqColl = Bio::SeqColl->new($seq1,$seq2); my $seqColl2 = Bio::SeqColl->new("a","b"); print "seqColl: ",ref($seqColl),"\n"; print "seqCOll2:",ref($seqColl2),"\n"; print Data::Dumper->Dump([$seqColl],["seqColl"]);

Output: seqColl: Bio::SeqColl seqCOll2: $seqColl = bless( { 'sequences' => [ bless( { 'sequence' => 'atgcag' }, 'Bio::Seq' ), 'sequence' => 'tatatataaa' }, 'Bio::Seq' ) ] }, 'Bio::SeqColl' );

Objects in Perl are implemented as hash tables linked to a certain package. The class of an object is the name of the package The bless command tells a hash table reference that it is of a certain class Use Data::Dumper to look at the data structure Use the command ref to see the class of a reference