SlideShare une entreprise Scribd logo
1  sur  22
Télécharger pour lire hors ligne
Advanced
    Modulinos
★        brian d foy
      The Perl Review
    Houston Perl Mongers
Files that work as
programs and modules
   at the same time
Unit testing

Code reüse

Distribution
# hello.pl
use v5.10;

say 'Hello World!';
% perl hello.pl
Hello World!
use v5.10;

run();

sub run {
 say 'Hello World!';
 }
% perl hello.pl
Hello World!
# Hello.pm
use v5.10;

run() unless caller;

sub run {
 say 'Hello World!';
 }
% perl hello.pl
Hello World!
% perl -MHello -e 1
%
package Hello;
use v5.10;

__PACKAGE__->run
   unless caller;

sub run {
 say 'Hello World!';
 }
use Test::More;
use Test::Output;

use_ok( 'Hello' );

stdout_ok(
   sub { Hello->run() },
   'Hello World!', ...
   );
package Hello;
use v5.10;

__PACKAGE__->run
   unless caller;

sub run {
 my( $self ) = @_;
 say { $self->fh }
   'Hello World!';
 }
package Hello;
use v5.10;
...;
sub fh { *STDOUT }
use Test::More;

use_ok( 'Hello' );

our $string;
{
open my $fh, '>', $string;
*Hello::fh = sub { $fh };
}

Hello->run;
is($string, 'Hello World!');
package Hello;
use v5.10;
...;
BEGIN {
my $fh = *STDOUT;
sub fh { $fh }
sub set_fh {
  $fh = ...;
}
use Test::More;

use_ok( 'Hello' );

open my $fh, '>', my $string;
Hello->set_fh( $fh );
Hello->run;
is($string, 'Hello World!');
% perl hello.pl
Hello World!
% perl hello.pl Houston
Hello World!
% perl hello.pl -m Texas
Hello World!
% perl hello.pl < partiers
Hello World!
Connect the
  command line to new()

% hello.pl -s Houston
                             ?
    use Hello;
                                 new()
    my $app->new(
      input => $in_fh,
      output => $out_fh,
      message => $message,
      );

    $app->greet;
sub run {
  my( $class, @args ) = @_;
  my %args =
    $class->process_args(@args);
  my $self = $class->new(%args);
  say { $self->fh }
    $self->message;
  }
sub process_args {
  require Getopt::Std;
  local @ARGV = @_;
  getopts('oim:', my %opts);

  $opts('o') //= *STDOUT;
  $opts('i') //= *STDIN;
  $opts('m') //= 'Hello World!';
  # left over @_?
  my %args = map {
    $opts_map{$_} => $opts{$_}
    } keys %opts;
  }
$app->new(
  input_fh => $in,
  output_fh => $out,
  message   => 'Hello World!'
  );
sub new {
  my( $class, %args ) = @_;
  my $self = bless {}, $class;

  foreach ( keys %args ) {
    # maybe more complicated
    $self->set( $_, $args{$_} );
    }

  return $self;
  }

Contenu connexe

Tendances

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
 
20 modules i haven't yet talked about
20 modules i haven't yet talked about20 modules i haven't yet talked about
20 modules i haven't yet talked about
Tatsuhiko Miyagawa
 
News of the Symfony2 World
News of the Symfony2 WorldNews of the Symfony2 World
News of the Symfony2 World
Fabien Potencier
 

Tendances (20)

Perl 6 by example
Perl 6 by examplePerl 6 by example
Perl 6 by example
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Perl5i
Perl5iPerl5i
Perl5i
 
Perl6 grammars
Perl6 grammarsPerl6 grammars
Perl6 grammars
 
PHP pod mikroskopom
PHP pod mikroskopomPHP pod mikroskopom
PHP pod mikroskopom
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
 
I, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 OverlordsI, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 Overlords
 
Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 Version
 
Teaching Your Machine To Find Fraudsters
Teaching Your Machine To Find FraudstersTeaching Your Machine To Find Fraudsters
Teaching Your Machine To Find Fraudsters
 
Melhorando sua API com DSLs
Melhorando sua API com DSLsMelhorando sua API com DSLs
Melhorando sua API com DSLs
 
20 modules i haven't yet talked about
20 modules i haven't yet talked about20 modules i haven't yet talked about
20 modules i haven't yet talked about
 
What's New in Perl? v5.10 - v5.16
What's New in Perl?  v5.10 - v5.16What's New in Perl?  v5.10 - v5.16
What's New in Perl? v5.10 - v5.16
 
Webrtc mojo
Webrtc mojoWebrtc mojo
Webrtc mojo
 
The most exciting features of PHP 7.1
The most exciting features of PHP 7.1The most exciting features of PHP 7.1
The most exciting features of PHP 7.1
 
News of the Symfony2 World
News of the Symfony2 WorldNews of the Symfony2 World
News of the Symfony2 World
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolvePHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolve
 
The Perl6 Type System
The Perl6 Type SystemThe Perl6 Type System
The Perl6 Type System
 
Perl6 in-production
Perl6 in-productionPerl6 in-production
Perl6 in-production
 
6 things about perl 6
6 things about perl 66 things about perl 6
6 things about perl 6
 
Refactoring using Codeception
Refactoring using CodeceptionRefactoring using Codeception
Refactoring using Codeception
 

En vedette (10)

The Whitespace in the Perl Community
The Whitespace in the Perl CommunityThe Whitespace in the Perl Community
The Whitespace in the Perl Community
 
Perl Conferences for Beginners
Perl Conferences for BeginnersPerl Conferences for Beginners
Perl Conferences for Beginners
 
CPAN Workshop, Chicago 2014
CPAN Workshop, Chicago 2014CPAN Workshop, Chicago 2014
CPAN Workshop, Chicago 2014
 
Create and upload your first Perl module to CPAN
Create and upload your first Perl module to CPANCreate and upload your first Perl module to CPAN
Create and upload your first Perl module to CPAN
 
Perl Power Tools - Saint Perl 6
Perl Power Tools - Saint Perl 6Perl Power Tools - Saint Perl 6
Perl Power Tools - Saint Perl 6
 
Tour of the Perl docs
Tour of the Perl docsTour of the Perl docs
Tour of the Perl docs
 
I ❤ CPAN
I ❤ CPANI ❤ CPAN
I ❤ CPAN
 
6 more things about Perl 6
6 more things about Perl 66 more things about Perl 6
6 more things about Perl 6
 
The Surprisingly Tense History of the Schwartzian Transform
The Surprisingly Tense History of the Schwartzian TransformThe Surprisingly Tense History of the Schwartzian Transform
The Surprisingly Tense History of the Schwartzian Transform
 
Perl 5.28 new features
Perl 5.28 new featuresPerl 5.28 new features
Perl 5.28 new features
 

Similaire à Advanced modulinos trial

2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
Hung-yu Lin
 
GettingStartedWithPHP
GettingStartedWithPHPGettingStartedWithPHP
GettingStartedWithPHP
Nat Weerawan
 
PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overview
jsmith92
 
Aura Project for PHP
Aura Project for PHPAura Project for PHP
Aura Project for PHP
Hari K T
 

Similaire à Advanced modulinos trial (20)

Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!
 
Descobrindo a linguagem Perl
Descobrindo a linguagem PerlDescobrindo a linguagem Perl
Descobrindo a linguagem Perl
 
Mojolicious
MojoliciousMojolicious
Mojolicious
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Durian: a PHP 5.5 microframework with generator-style middleware
Durian: a PHP 5.5 microframework with generator-style middlewareDurian: a PHP 5.5 microframework with generator-style middleware
Durian: a PHP 5.5 microframework with generator-style middleware
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
 
Web 8 | Introduction to PHP
Web 8 | Introduction to PHPWeb 8 | Introduction to PHP
Web 8 | Introduction to PHP
 
GettingStartedWithPHP
GettingStartedWithPHPGettingStartedWithPHP
GettingStartedWithPHP
 
PHP Unit Testing
PHP Unit TestingPHP Unit Testing
PHP Unit Testing
 
07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
Perl Web Client
Perl Web ClientPerl Web Client
Perl Web Client
 
Improving Dev Assistant
Improving Dev AssistantImproving Dev Assistant
Improving Dev Assistant
 
Blog Hacks 2011
Blog Hacks 2011Blog Hacks 2011
Blog Hacks 2011
 
Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010
 
Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010
 
PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overview
 
Nigel hamilton-megameet-2013
Nigel hamilton-megameet-2013Nigel hamilton-megameet-2013
Nigel hamilton-megameet-2013
 
Aura Project for PHP
Aura Project for PHPAura Project for PHP
Aura Project for PHP
 
Symfony2 - OSIDays 2010
Symfony2 - OSIDays 2010Symfony2 - OSIDays 2010
Symfony2 - OSIDays 2010
 

Plus de brian d foy

Plus de brian d foy (15)

Conferences for Beginners presentation
Conferences for Beginners presentationConferences for Beginners presentation
Conferences for Beginners presentation
 
20 years in Perl
20 years in Perl20 years in Perl
20 years in Perl
 
PrettyDump Perl 6 (London.pm)
PrettyDump Perl 6 (London.pm)PrettyDump Perl 6 (London.pm)
PrettyDump Perl 6 (London.pm)
 
Dumping Perl 6 (French Perl Workshop)
Dumping Perl 6 (French Perl Workshop)Dumping Perl 6 (French Perl Workshop)
Dumping Perl 6 (French Perl Workshop)
 
Perl v5.26 Features (AmsterdamX.pm)
Perl v5.26 Features (AmsterdamX.pm)Perl v5.26 Features (AmsterdamX.pm)
Perl v5.26 Features (AmsterdamX.pm)
 
Dumping Perl 6 (AmsterdamX.pm)
Dumping Perl 6 (AmsterdamX.pm)Dumping Perl 6 (AmsterdamX.pm)
Dumping Perl 6 (AmsterdamX.pm)
 
Reverse Installing CPAN
Reverse Installing CPANReverse Installing CPAN
Reverse Installing CPAN
 
Backward to DPAN
Backward to DPANBackward to DPAN
Backward to DPAN
 
Perl docs {sux|rulez}
Perl docs {sux|rulez}Perl docs {sux|rulez}
Perl docs {sux|rulez}
 
Why I Love CPAN
Why I Love CPANWhy I Love CPAN
Why I Love CPAN
 
What's wrong with the perldocs
What's wrong with the perldocsWhat's wrong with the perldocs
What's wrong with the perldocs
 
Frozen Perl 2011 Keynote
Frozen Perl 2011 KeynoteFrozen Perl 2011 Keynote
Frozen Perl 2011 Keynote
 
brian d foy
brian d foybrian d foy
brian d foy
 
Making My Own CPAN
Making My Own CPANMaking My Own CPAN
Making My Own CPAN
 
Making My Own CPAN
Making My Own CPANMaking My Own CPAN
Making My Own CPAN
 

Dernier

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Dernier (20)

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
 
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
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
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?
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
[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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
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?
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

Advanced modulinos trial