Presentation is loading. Please wait.

Presentation is loading. Please wait.

Perl for biologists A. Emerson, Perl for Biologists PERL More references, complex data types and objects.

Similar presentations


Presentation on theme: "Perl for biologists A. Emerson, Perl for Biologists PERL More references, complex data types and objects."— Presentation transcript:

1 Perl for biologists A. Emerson, Perl for Biologists PERL More references, complex data types and objects

2 Perl for biologists A. Emerson, Perl for Biologists References in Perl  An alternative mechanism of using variables such as arrays or associative arrays.  Useful to think of a reference as the address of the object in memory.  Often very efficient and convenient because references are always stored in scalar variables, regardless of the size or complexity of the thing being referenced.  Can be used to avoid the difficulty of passing multiple arrays in or out of subroutines.  In Perl you can take a reference of just about anything, including subroutines.

3 Perl for biologists A. Emerson, Perl for Biologists Creating references and $dna_ref = # storing a reference in scalar # implicit references $ref_ref = \$dna_ref; # reference of a reference # $base = $$dna_ref[1]; # careful! dereferencing occurs $$dna_ref[0]=‘C’; # before array lookup. (We are not # $base = $dna_ref->[1]; # alternative notation (clearer) It may help to consider a reference as an alias of the variable being referenced.

4 Perl for biologists A. Emerson, Perl for Biologists Two dimensional arrays  References can be used to create data types not present in standard Perl, e.g. matrices @row3=(1.0,0.0,1.0); # perl simulation of a 2-d matrix(3,3) for(my $i=0;$i<3; $i++) { for(my $j=0;$j<3;$j++) { print “$matrix[$i]->[$j]; } print “\n”; }  Strictly speaking this is a an array of references (to other arrays) but is more flexible because the rows can be of different lengths  The above solution though creates unnecessary arrays etc) – we would like to create the matrix directly.

5 Perl for biologists A. Emerson, Perl for Biologists Anonymous arrays and hashes  Perl provides an alternative method for creating arrays and hashes: # standard array # anonymous definition using a reference $dna = [ A, A, A, A ]; # NB [] brackets! $dna->[0] = ‘T’;  The new array does not have a name (it is “anonymous”) but can be referenced.

6 Perl for biologists A. Emerson, Perl for Biologists Using anonymous arrays # defining a 2D matrix in one = ( [1.0,0.0,1.0], [0.0,1.0,0.0], [1.0,0.0,1.0]); print “$matrix[1]->[1]\n”; # or even $matrix = [ [1.0,0.0,1.0], [0.0,1.0,0.0], [1.0,0.0,1.0]]; $matrix->[1]->[1]=2.0;

7 Perl for biologists A. Emerson, Perl for Biologists Using anonymous arrays # defining a 2D matrix in one = ( [1.0,0.0,1.0], [0.0,1.0,0.0], [1.0,0.0,1.0]); print “$matrix[1]->[1]\n”; # alternatively print “$matrix[1][1]\n”;  Between two subscripts the -> is optional  Similarly for 3d arrays, hashes,etc.

8 Perl for biologists A. Emerson, Perl for Biologists Using anonymous $bref=$aref; $aref->[1]=99; print  Assigning one array ref to another doesn’t copy the array:  -you can use an anonymous array $aref->[1]=99; print

9 Perl for biologists A. Emerson, Perl for Biologists Using anonymous arrays and hashes # Anonymous hashes $code={AAA=>stop, CGT=> ‘ser’}; $code->{UAC}=‘tyr’; foreach $key (keys %{$code}) { print $code->{$key},”\n”; }  Anonymous hashes can be similarly created using { }:

10 Perl for biologists A. Emerson, Perl for Biologists Using anonymous arrays and hashes You can mix anonymous arrays and hashes to create compilcated structures: my $planck={home => 'csc', name=>"PLANCK", accounts=>['csc07141','csc18709','csc18091','csc11014'],all ocated => }; my $muheart={home =>’epcc’,name=>”MuHeart”, accounts=>[‘hpx001’,’hpx0002’,’hpx0003’],allocated=>100000} ; foreach $project { print $project->{name},”\n”; foreach $acc { print “$project->{home} $acc\n”; } notice

11 Perl for biologists A. Emerson, Perl for Biologists References to functions and other things  You can make references to anything, including scalars, functions, and other references. # reference to a sub $coderef = sub { print "Boink!\n" }; $coderef->();  This looks a bit strange at first but becomes important in object-oriented programming.

12 Perl for biologists A. Emerson, Perl for Biologists Objects  Simple scalars, arrays, hashes or more complex structures are often given the generic term object.  Objects can created, destroyed, copied or passed between one part of the program and another. They can be combined to give other objects and you can access them via references.  In languages such as C or Fortran (90+) new objects can defined by creating new data types which extend the built in set of int, double, real, character, etc. /* C program */ typedef struct { float i; /*real part*/ float j; /*imag part */ } complex; complex a,b;

13 Perl for biologists A. Emerson, Perl for Biologists Objects  In Object-oriented programming languages (e.g. C++ or Java) objects don’t just contain data but also program code which governs how the object interacts with the rest of the program.  The code in the form of method functions can be used to create the object (“the constructor”) or to operate on other objects.  The program is no longer written as a set of sequential instructions but instead a collection of interacting objects. Often a very convenient and natural way of representing a programming problem.

14 Perl for biologists A. Emerson, Perl for Biologists Implementation of objects State is usually held as local variables (also called properties), quantities sometimes not visible outside the object (data hiding) Behaviour controlled by method functions or subroutines which act on the local variables and interface with the outside. STATE BEHAVIOUR

15 Perl for biologists A. Emerson, Perl for Biologists C++ objects // template definition class CRectangle { // state data int x, y; public: // method functions void setvalues (int a,int b){x=a;y=b;} int area (){ return (x*y);} } // main code CRectangle recta, rectb; recta.setvalues(3,4); cout << recta.area() << endl;

16 Perl for biologists A. Emerson, Perl for Biologists OOP – key concepts  Classes  The templates used to define the objects. Note that defining the class does not actually create the object.  Instantiation  When the object is created (an object is an instance of a class).  Properties  Data about the object itself. Can be private (not directly accessible by other objects ) or public (accessible).  Method functions  Program code used to define the behaviour or functionality of the object. Special functions called constructors create the object.  Inheritance  Deriving new classes from previously defined classes. Saves programming effort and can create object heirarchies.

17 Perl for biologists A. Emerson, Perl for Biologists Key feature of OOP - Inheritance Important ability of any OOP is the ability to derive one object from a more general class of related objects: this is called inheritance. A standard eukaryotic cell nucleus, cell membrane, cytoplasm, alive or dead undergoes division, makes proteins from DNA nerve cell skin cell white blood cell

18 Perl for biologists A. Emerson, Perl for Biologists OOP and Perl  Perl was never designed as a true OOP language. The implementation is rather ad-hoc, and the OOP syntax is non-standard.  Creating Perl objects is rarely done but it is important to know how to use them because library packages (e.g. from CPAN) often use them.  Using and managing Perl objects requires the heavy use of references.

19 Perl for biologists A. Emerson, Perl for Biologists PERL Objects - Example # Perl Database object package # (contains definitions of the DB object) use DBI; my $dsn=“mysql.cineca.it –p 2222”; my $user=“test01”; my $password=“testtest”; # create a DB object, referenced by $dbh my $dbh = DBI->connect($dsn, $user, $password, { RaiseError => 1, AutoCommit => 0 });..... do something $dbh->commit; $dbh->disconnect;

20 Perl for biologists A. Emerson, Perl for Biologists Summary  References, together with anonymous arrays and hashes, can be used to create complex data structures which arent present in standard Perl (e.g. 2D arrays or tables).  An important use of references is in object –oriented programming. Most Perl programmers do not write object definitions in Perl but libraries (e.g. BioPerl) often use them.


Download ppt "Perl for biologists A. Emerson, Perl for Biologists PERL More references, complex data types and objects."

Similar presentations


Ads by Google