08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
Object Oriented Perl Overview
1. Object Oriented Perl Overview
Review of references... example from hash of
hashes
Overview of why use objects (again)
Brian O'Connor Simple example, convert Lincoln's module to an
UCLA object-oriented module
boconnor@ucla.edu
What are objects really?
Inheritance
Simple inheritance example from Simon's code
The Long Way
Hash of Hashes Review
This is the long
(perhaps clearer)
Hash_1
$hash_ref value
way...
key (hash_ref) Hash_2
*
GeneA * key value
seq ccta...
GeneB *
length 67
GeneC *
gc% 52%
2. The Long Way
If we dump $data, which is a hashref, we get Hash of Hashes Review
the following:
$VAR1 = { Hash_1
'GeneC' => { $hash_ref value
'length' => 10, key (hash_ref) Hash_2
'gc' => '0.5',
*
'seq' => 'cccggattat' GeneA * key value
},
'GeneB' => { seq ccta...
'length' => 10,
'gc' => '0.5',
GeneB *
'seq' => 'cccggattat' length 67
},
'GeneA' => {
GeneC *
'length' => 10, gc% 52%
'gc' => '0.5',
'seq' => 'cccggattat'
}
};
The Short Way The Short Way
Compare that with the way I showed you You can see the structure produced is the same:
yesterday:
$VAR1 = {
'GeneC' => {
'length' => 10,
'gc' => '0.5',
'seq' => 'cccggattat'
},
'GeneB' => {
'length' => 10,
'gc' => '0.5',
'seq' => 'cccggattat'
},
'GeneA' => {
'length' => 10,
'gc' => '0.5',
'seq' => 'cccggattat'
}
};
3. Why Objects Why Object Oriented Perl?
Small App Small App
Small App
MONOLITHIC vs. Obj 1 Obj 3 Obj 1 Obj 3
Obj 2 Small App Obj 2
Look Inside an Object Modules vs. Objects
First, let's convert Lincoln's MySequence module
Obj1
from an earlier lecture to an object-oriented
module
method_1
The difference is pretty clear:
Data
method_2 modules let you import variables and subroutines into
your current program when you say “use”
method_3
objects are encapsulated, you get a reference to one
(usually using “new”) and call methods on them
directly
4. The Original Module Code to Call MySequence
the subroutines in this module are now available
in your code
Exporter
What we
export
Works as You Expect Conflicts
By saying use MySequence; all the subroutines What happens when you have conflicts for
and data MySequence exports is available within subroutines or variables?
your program
[boconnor@dhcp10-51 module]$ perl test.pl
original = gattccggatttccaaagggttcccaatttggg
complement = cccaaattgggaaccctttggaaatccggaatc
5. Conflicts
The Object Oriented Way
What happens when you have conflicts for
subroutines or variables?
Let's recode this as an object-oriented module
[boconnor@dhcp10-51 module]$ perl test.pl
Avoids pollution of your namespace
HI!! Key terms
original = gattccggatttccaaagggttcccaatttggg
complement = 1 abstraction
encapsulation
The new subroutine interferes with the
MySequence subroutine
Recall from Yesterday Recall from Yesterday
How to create an object from an Object Oriented How to then call methods on that object...
Module...
#!/usr/bin/perl -w $sequence->subseq(1,40);
use strict; $sequence->id();
use Bio::PrimarySeq; $sequence->desc();
my $sequence = Bio::PrimarySeq->new(
-seq => 'gattaca',
-id => 'oligo234',
-alphabet => 'dna'
);
6. The Original Module The Object Oriented Module
Need to When you make a method
call on an OO Module the
eliminate the first argument is the class
Exporter
code
Need to
include a
“new”
method
The Object Oriented Module The Object Oriented Module
You create a new hash bless associates this new
reference ($self) and this bit of memory with a class
will store all the objects type (i.e. package to call
data methods with)
7. What Does bless Mean? What Does bless Mean?
an example... in the debugger
an example...
$obj = { name => 'mini', color =>
$obj = { name => 'mini', color => 'yellow' };
'yellow' }; x $obj
print ref($obj), “ ”, $obj->{name}, “n”;
bless ($obj, “Fruit::Banana”); Fruit::Banana=HASH(0x8ec7980)
print ref($obj), “ ”, $obj->{name}, “n”; 'color' => 'yellow'
'name' => 'mini'
HASH mini
Fruit::Banana mini What is an object then? A
hashref that stores data and is
associated with a package
The Object Oriented Module The Object Oriented Module
The object is now created
and returned to the
program that called new.
When called in an object
context the object is
automatically passed in at
the first argument
8. What is $self? Code to Call MySequence
the actual object you're calling this method from Let's take a look at the code that uses the new OO
allows you to manipulate the data within that MySequence module
specific object instance (it's just a hashref)
compare this to new() and other method calls
like it
Output is Identical! Why Object Oriented Perl?
Identical output to the previous version
Small App
[boconnor@dhcp10-51 oo_module]$ perl test.pl Small App
original = gattccggatttccaaagggttcccaatttggg
complement = cccaaattgggaaccctttggaaatccggaatc
Obj 1 Obj 3
And you can create your own reversec subroutine log log
and not have it conflict with the MySequence Obj 2
Small App
method! log
9. Inheritance Inheritance
In this example every object has a log()
method Independent parent Base
& Redundant log
Wouldn't it be nice to only write this once?
becomes
Solution: Inheritance! Obj 1
log child Obj 1 Obj 2
Obj 2
log Neither actually
implements a log
method
More Generally Simon's Example: The Machines!
Let's take a look at the implementation of the
superclass Fruit name, color, shape
following hierarchy
isa
isa
Machine voltage
name, color, shape,
subclass Grape Banana peel
isa
isa
Think of Banana inherits voltage voltage
Refrigerator ATM
inheritance as an name, color & temp cash
“isa” relationship shape
banana adds peel
10. Simon's Example: The Machines! Simon's Example: The Machines!
We want to implement Machine and Refrigerator The means that we can create a Refrigerator
so that Machine defines the voltage() method object and call either voltage() or temp()
and Refrigerator defines the temp() method on it
Machine voltage Machine voltage
isa
isa
isa
isa
voltage voltage voltage voltage
Refrigerator ATM Refrigerator ATM
temp cash temp cash
Let's Start with test.pl Let's Start with test.pl
“use” what you want Always include a method to
to create before you create a new object.
call new Call it using this OOP syntax.
* differs from calling Machine::Refrigerator::new()
11. The Refrigerator The Refrigerator
Recall, the Refrigerator
needs to do 2 things for magic
the client:
get/set a temperature
get/set a voltage magic
The Refrigerator The Refrigerator
This says, use the Machine, as the root
“Machine” module class, handles the
as a base class. creation and
Everything a initialization of the
Machine can do so object via its new
can a Refrigerator. method. This is how
you call it.
12. The Machine
The Refrigerator
Call new() to Create a Refrigerator
magic
It returns a new object
of type
Machine::Refrigerator!!
magic
The Machine The Machine
Call new() to Create a Refrigerator Call new() to Create a Refrigerator
so the object ($self) is just
a reference to an empty
hash! Just like in the
How does it become a MySequence example,
Machine::Refrigerator? the bless command
associates a hashref with
a class
(Machine::Refrigerator
in this case)
13. The Refrigerator Finish back at test.pl
$r (a Machine::Refrigerator)
has both the voltage()
method (from Machine) and it's
Finish setting up own temp() method
Refrigerator
specific information
and return the new
object $self
Look at the Output Lessons
You can call both temp() and voltage() on When you call a method this way:
the $r object which is of type
Machine::Refrigerator->new(
Machine::Refrigerator temp => 20,
voltage => 110 );
my $self = $class->SUPER::new(@_);
[boconnor@dhcp10-51]$ perl test.pl
Start temp: 20
Start voltage: 110 The first var is a string of the class (package)
New temp: 45 name you're calling on... In both cases it's
“Machine::Refrigerator”
14. Lessons Lessons
When you call a method this way (through an The returned object is a bit of memory (a hashref)
object): associated with a given class and it's methods
$r->temp();
Here's a dump in the debugger of the $r object:
The first var is a reference to the object itself so
you can get/set data within it... it's just a
“blessed” hashref! DB<2> x $r
Machine::Refrigerator=HASH(0x94e3bd8)
sub temp { 'temp' => 20
my $self = shift;
'voltage' => 110
...
}
So Now You... For More Information
Can use references Perl Cookbook, Programming Perl
Can use other people's objects perlobj and perlref man pages
Can create objects of your own Google
Can create complex object trees through Simon's Object Tutorial, try implementing the
inheritance ATM class