SlideShare une entreprise Scribd logo
1  sur  20
Perl From Ground level and Up Lecture 2 October 22 2007 By Shmuel Fomberg
Parameters Passing ,[object Object],October 22 2007 Shmuel Fomberg func(5, $x, (7, 4), [$y, %h]); # @_ = (5, $x, 7, 4, [$y, %h]) sub func {   my $x1 = shift; # $x1 is 5   $_[0] = 3; # $x now is 3! (do not use this, normally)   my ($x2, $x3) = @_; # multiple params   …
Perl script life cycle ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],October 22 2007 Shmuel Fomberg
Perl script life cycle October 22 2007 Shmuel Fomberg #!/usr/bin/perl -w use strict; BEGIN {print &quot;one &quot;} use A; END {print &quot;two &quot;} print &quot;three &quot;; package A; BEGIN {print &quot;four &quot;} END {print &quot;five &quot;} print &quot;six &quot;; 1; #  <- note here Output: one four six three two five  Test.pl A.pm
What is a Class? ,[object Object],[object Object],October 22 2007 Shmuel Fomberg Class->func(…); my $name = ‘Class’; $name->func(…); In both cases: Class::func(‘Class’, …) Can even do: my $fname = ‘func’; $name->$fname(…);
What is a Class? October 22 2007 Shmuel Fomberg ,[object Object],[object Object],package Class; our $var = 5; …  elsewhere in the program: print $Class::var;
Class inheritance ,[object Object],[object Object],October 22 2007 Shmuel Fomberg package Class; our @ISA = qw{ClassP1 ClassP2}; use base qw{ClassP1 ClassP2}; func(…); #will search …  elsewhere in the program: Class->func(…); #will search Class::func(…); # will not search Class->ClassP2::func(…); # explicit
Class inheritance ,[object Object],October 22 2007 Shmuel Fomberg Class->can(‘func’); Class->isa(‘ClassP1); UNIVERSIAL::isa(‘Class’, ‘ClassP1’); ,[object Object],[object Object],[object Object],package Class; SubClass->SUPER::func(…);
The SUPER meta class October 22 2007 Shmuel Fomberg A B C D E $class->func(...) $class->SUPER::func(...) package C; sub mysub {   my $class = shift;   # $class eq ‘E’   …..
What is an Object? ,[object Object],[object Object],[object Object],[object Object],October 22 2007 Shmuel Fomberg my $self = bless {}, ‘Class’; ,[object Object],[object Object],my $self = shift; my ($self, $param1, …) = @_;
What is an Object? ,[object Object],October 22 2007 Shmuel Fomberg $obj->{var1} = 5; ,[object Object],$obj->isa(‘ClassP1’); if ref($obj) and $obj->isa(‘ClassP1’); $obj->func(…); $self->SUPER::func(…); Class::func($obj, …);
Creating an Object ,[object Object],October 22 2007 Shmuel Fomberg sub new {   my ($class, params…) = @_;   my $self = { keys and values… };   return bless $self, $class; } ,[object Object],sub new {   my ($class, params…) = @_;   $self = $class->Parent::new(params…);   # do more initialization   return $self; }
Destroying an Object ,[object Object],[object Object],[object Object],October 22 2007 Shmuel Fomberg sub DESTROY {   my $self = shift;   # do whatever   $self->SUPER::DESTROY(); }
Problems? October 22 2007 Shmuel Fomberg NOT SCALABLE Object variable are just keys in hash - spelling error will created bugs You have to know and hard-code the Class-variable package  The new will not support multi-inheritance
How Should it be done ,[object Object],October 22 2007 Shmuel Fomberg package Root; use base qw/Class::Accessor Class::Data::Inheritable/; sub new {   my ($class, @params) = @_;   my $self = bless {}, $class;   $self->_Init(@params);   return $self; } sub _Init {} sub DESTROY {}
How Should it be done ,[object Object],October 22 2007 Shmuel Fomberg package MyClass; use base qw/Root/; __PACKAGE__->mk_accessors(qw(name role salary)); sub _Init {   my ($self, $salary, $name) = @_;   $self->SUPER::_Init($name);   $self->salary($salary); } sub DESTROY { # if needed   my $self = shift;   ... do something ...   $self->SUPER::DESTORY; }
How Should it be done ,[object Object],October 22 2007 Shmuel Fomberg package MyClass; use base qw/Root/; Stuff->mk_classdata(‘Color'); # Declare the location of the data file for this class. Stuff->Color(‘blue'); # Or, all in one shot: Stuff->mk_classdata(Color => ‘blue');
Unit Test ,[object Object],[object Object],[object Object],[object Object],October 22 2007 Shmuel Fomberg 5 == sum(3,2)
Perl Standard Unit Test ,[object Object],October 22 2007 Shmuel Fomberg use Test::Simple tests => 2; my $foo = &quot;x&quot;; ok( $foo eq &quot;x&quot;, 'foo is X' ); ok( $foo ne &quot;Y&quot;, 'foo is not Y'); 1..2 ok 1 - foo is X ok 2 - foo is not Y
A bit more advanced techniques ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],October 22 2007 Shmuel Fomberg

Contenu connexe

Tendances

Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell Script
Dr.Ravi
 
Unix And C
Unix And CUnix And C
Unix And C
Dr.Ravi
 
News of the Symfony2 World
News of the Symfony2 WorldNews of the Symfony2 World
News of the Symfony2 World
Fabien Potencier
 
Airlover 20030324 1
Airlover 20030324 1Airlover 20030324 1
Airlover 20030324 1
Dr.Ravi
 

Tendances (20)

Perl basics for pentesters part 2
Perl basics for pentesters part 2Perl basics for pentesters part 2
Perl basics for pentesters part 2
 
Perl5i
Perl5iPerl5i
Perl5i
 
Perl 5.10
Perl 5.10Perl 5.10
Perl 5.10
 
Cpsh sh
Cpsh shCpsh sh
Cpsh sh
 
Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basics
 
Replacing "exec" with a type and provider: Return manifests to a declarative ...
Replacing "exec" with a type and provider: Return manifests to a declarative ...Replacing "exec" with a type and provider: Return manifests to a declarative ...
Replacing "exec" with a type and provider: Return manifests to a declarative ...
 
Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell Script
 
Syntax
SyntaxSyntax
Syntax
 
BSDM with BASH: Command Interpolation
BSDM with BASH: Command InterpolationBSDM with BASH: Command Interpolation
BSDM with BASH: Command Interpolation
 
Introduction in php part 2
Introduction in php part 2Introduction in php part 2
Introduction in php part 2
 
Findbin libs
Findbin libsFindbin libs
Findbin libs
 
Metadata-driven Testing
Metadata-driven TestingMetadata-driven Testing
Metadata-driven Testing
 
Unix And C
Unix And CUnix And C
Unix And C
 
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
 
Bag of tricks
Bag of tricksBag of tricks
Bag of tricks
 
Hypers and Gathers and Takes! Oh my!
Hypers and Gathers and Takes! Oh my!Hypers and Gathers and Takes! Oh my!
Hypers and Gathers and Takes! Oh my!
 
Parsing JSON with a single regex
Parsing JSON with a single regexParsing JSON with a single regex
Parsing JSON with a single regex
 
Introduction in php
Introduction in phpIntroduction in php
Introduction in php
 
News of the Symfony2 World
News of the Symfony2 WorldNews of the Symfony2 World
News of the Symfony2 World
 
Airlover 20030324 1
Airlover 20030324 1Airlover 20030324 1
Airlover 20030324 1
 

Similaire à Perl from the ground up: objects and testing

Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
Kang-min Liu
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
Kang-min Liu
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
Stoyan Stefanov
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
jsmith92
 
Barely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationBarely Legal Xxx Perl Presentation
Barely Legal Xxx Perl Presentation
Attila Balazs
 

Similaire à Perl from the ground up: objects and testing (20)

Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Feature Flags Are Flawed: Let's Make Them Better
Feature Flags Are Flawed: Let's Make Them BetterFeature Flags Are Flawed: Let's Make Them Better
Feature Flags Are Flawed: Let's Make Them Better
 
Constructive Destructor Use
Constructive Destructor UseConstructive Destructor Use
Constructive Destructor Use
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
 
Php Oop
Php OopPhp Oop
Php Oop
 
Feature flagsareflawed
Feature flagsareflawedFeature flagsareflawed
Feature flagsareflawed
 
Demystifying Object-Oriented Programming - Midwest PHP
Demystifying Object-Oriented Programming - Midwest PHPDemystifying Object-Oriented Programming - Midwest PHP
Demystifying Object-Oriented Programming - Midwest PHP
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
 
Subroutines
SubroutinesSubroutines
Subroutines
 
PHP Classes and OOPS Concept
PHP Classes and OOPS ConceptPHP Classes and OOPS Concept
PHP Classes and OOPS Concept
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
mro-every.pdf
mro-every.pdfmro-every.pdf
mro-every.pdf
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHP
 
10 classes
10 classes10 classes
10 classes
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
 
Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4
 
The Origin of Lithium
The Origin of LithiumThe Origin of Lithium
The Origin of Lithium
 
PHP Unit Testing
PHP Unit TestingPHP Unit Testing
PHP Unit Testing
 
Barely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationBarely Legal Xxx Perl Presentation
Barely Legal Xxx Perl Presentation
 
Perl Bag of Tricks - Baltimore Perl mongers
Perl Bag of Tricks  -  Baltimore Perl mongersPerl Bag of Tricks  -  Baltimore Perl mongers
Perl Bag of Tricks - Baltimore Perl mongers
 

Dernier

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Dernier (20)

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 

Perl from the ground up: objects and testing

  • 1. Perl From Ground level and Up Lecture 2 October 22 2007 By Shmuel Fomberg
  • 2.
  • 3.
  • 4. Perl script life cycle October 22 2007 Shmuel Fomberg #!/usr/bin/perl -w use strict; BEGIN {print &quot;one &quot;} use A; END {print &quot;two &quot;} print &quot;three &quot;; package A; BEGIN {print &quot;four &quot;} END {print &quot;five &quot;} print &quot;six &quot;; 1; # <- note here Output: one four six three two five Test.pl A.pm
  • 5.
  • 6.
  • 7.
  • 8.
  • 9. The SUPER meta class October 22 2007 Shmuel Fomberg A B C D E $class->func(...) $class->SUPER::func(...) package C; sub mysub { my $class = shift; # $class eq ‘E’ …..
  • 10.
  • 11.
  • 12.
  • 13.
  • 14. Problems? October 22 2007 Shmuel Fomberg NOT SCALABLE Object variable are just keys in hash - spelling error will created bugs You have to know and hard-code the Class-variable package The new will not support multi-inheritance
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.