SlideShare a Scribd company logo
1 of 14
Download to read offline
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%
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'
                                                                              }
                                                                };
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
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
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'
            );
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)
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
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
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
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()
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.
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)
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”
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

More Related Content

More from tutorialsruby

&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />tutorialsruby
 
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>tutorialsruby
 
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />tutorialsruby
 
Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269tutorialsruby
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008tutorialsruby
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008tutorialsruby
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheetstutorialsruby
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheetstutorialsruby
 

More from tutorialsruby (20)

&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
 
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
 
xhtml-documentation
xhtml-documentationxhtml-documentation
xhtml-documentation
 
xhtml-documentation
xhtml-documentationxhtml-documentation
xhtml-documentation
 
CSS
CSSCSS
CSS
 
CSS
CSSCSS
CSS
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
 
HowTo_CSS
HowTo_CSSHowTo_CSS
HowTo_CSS
 
HowTo_CSS
HowTo_CSSHowTo_CSS
HowTo_CSS
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
 

Recently uploaded

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 

Recently uploaded (20)

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 

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