Presentation is loading. Please wait.

Presentation is loading. Please wait.

References and Objects

Similar presentations


Presentation on theme: "References and Objects"— Presentation transcript:

1 References and Objects

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

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

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

5 the backslash in front of a variable gives you a reference to that variable

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

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

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

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

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

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

12 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

13 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.

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

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

16 "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

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

18 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;

19 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"]);

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

21 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");

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

23 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"]);

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

25 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


Download ppt "References and Objects"

Similar presentations


Ads by Google