SlideShare a Scribd company logo
★   Advanced
    Modulinos
       brian d foy
     The Perl Review
     YAPC::NA 2012
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!
STDIN   $0   STDOUT
@ARGV
        %ENV         exit code
Starting
use v5.10;

run();

sub run {
 say 'Hello World!';
 }
% perl hello.pl
Hello World!
% perl -e 'require q(hello.pl)'
Hello World!
# Hello.pm
use v5.10;

run() unless caller;

sub run {
 say 'Hello World!';
 }

_ _PACKAGE_ _
% perl Hello.pm
Hello World!
% perl -MHello -e 1
%
package Hello;
use v5.10;

__PACKAGE__->run
   unless caller;

sub run {
 say 'Hello World!';
 }

_ _PACKAGE_ _
use Test::More;
use Test::Output;

use_ok( 'Hello' );

stdout_ok(
   sub { Hello->run() },
   "Hello World!n", ...
   );
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!n");
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!n");
% perl hello.pl
Hello World!
% perl hello.pl Chicago
Hello World!
% perl hello.pl -m Rahm
Hello World!
% perl hello.pl < aldermen
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;
  }
Stopping
#!perl
...;
...;
...;

exit(0);
sub run {
  my( $class, @args ) = @_;
  my $object = eval {
   ...;
   Result->new( code => 0 );
   } or $@;
  exit( $object->code );
  }
sub some_sub {
  ...;
   die
     Result->new( code => 15 );
  ...;
  }
sub run {
  my( $class, @args ) = @_;
  my $object = eval {
   ...;
   Result->new( code => 0 );
   };
  exit( $error_object->code );
  }
Testing
run() unless caller;
UNITCHECK {
  if($ENV{TEST_HARNESS}){
    __PACKAGE__->run_tests;
    }
  elsif( ! caller ) {
    __PACKAGE__->run;
    }
  }
Docs
UNITCHECK {
  if($ENV{TEST_HARNESS}){
    __PACKAGE__->run_tests;
    }
  elsif($ENV{PERLDOC}){
    __PACKAGE__->show_docs;
    }
  elsif( ! caller ) {
    __PACKAGE__->run;
    }
  }

More Related Content

What's hot (20)

ODP
Perl5i
Marcos Rebelo
 
ODP
PHP pod mikroskopom
Saša Stamenković
 
PDF
Debugging: Rules And Tools - PHPTek 11 Version
Ian Barber
 
PDF
Perl 6 by example
Andrew Shitov
 
PDF
Teaching Your Machine To Find Fraudsters
Ian Barber
 
PDF
20 modules i haven't yet talked about
Tatsuhiko Miyagawa
 
KEY
Good Evils In Perl (Yapc Asia)
Kang-min Liu
 
PDF
Perl6 grammars
Andrew Shitov
 
PDF
Melhorando sua API com DSLs
Augusto Pascutti
 
PPTX
PHP PPT FILE
AbhishekSharma2958
 
PDF
The most exciting features of PHP 7.1
Zend by Rogue Wave Software
 
PDF
I, For One, Welcome Our New Perl6 Overlords
heumann
 
PDF
What's New in Perl? v5.10 - v5.16
Ricardo Signes
 
PDF
News of the Symfony2 World
Fabien Potencier
 
PDF
6 things about perl 6
brian d foy
 
PDF
PHPCon 2016: PHP7 by Witek Adamus / XSolve
XSolve
 
KEY
My Development Story
Takahiro Fujiwara
 
PDF
The Perl6 Type System
abrummett
 
PDF
Refactoring using Codeception
Jeroen van Dijk
 
PDF
Introdução ao Perl 6
garux
 
PHP pod mikroskopom
Saša Stamenković
 
Debugging: Rules And Tools - PHPTek 11 Version
Ian Barber
 
Perl 6 by example
Andrew Shitov
 
Teaching Your Machine To Find Fraudsters
Ian Barber
 
20 modules i haven't yet talked about
Tatsuhiko Miyagawa
 
Good Evils In Perl (Yapc Asia)
Kang-min Liu
 
Perl6 grammars
Andrew Shitov
 
Melhorando sua API com DSLs
Augusto Pascutti
 
PHP PPT FILE
AbhishekSharma2958
 
The most exciting features of PHP 7.1
Zend by Rogue Wave Software
 
I, For One, Welcome Our New Perl6 Overlords
heumann
 
What's New in Perl? v5.10 - v5.16
Ricardo Signes
 
News of the Symfony2 World
Fabien Potencier
 
6 things about perl 6
brian d foy
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
XSolve
 
My Development Story
Takahiro Fujiwara
 
The Perl6 Type System
abrummett
 
Refactoring using Codeception
Jeroen van Dijk
 
Introdução ao Perl 6
garux
 

Viewers also liked (10)

PDF
The Whitespace in the Perl Community
brian d foy
 
PDF
CPAN Workshop, Chicago 2014
brian d foy
 
PDF
Perl Conferences for Beginners
brian d foy
 
PDF
Tour of the Perl docs
brian d foy
 
PDF
Perl Power Tools - Saint Perl 6
brian d foy
 
PDF
6 more things about Perl 6
brian d foy
 
PDF
I ❤ CPAN
brian d foy
 
PDF
Create and upload your first Perl module to CPAN
brian d foy
 
PDF
The Surprisingly Tense History of the Schwartzian Transform
brian d foy
 
PDF
Perl 5.28 new features
brian d foy
 
The Whitespace in the Perl Community
brian d foy
 
CPAN Workshop, Chicago 2014
brian d foy
 
Perl Conferences for Beginners
brian d foy
 
Tour of the Perl docs
brian d foy
 
Perl Power Tools - Saint Perl 6
brian d foy
 
6 more things about Perl 6
brian d foy
 
I ❤ CPAN
brian d foy
 
Create and upload your first Perl module to CPAN
brian d foy
 
The Surprisingly Tense History of the Schwartzian Transform
brian d foy
 
Perl 5.28 new features
brian d foy
 
Ad

Similar to Advanced modulinos (20)

PDF
Perl6 in-production
Andrew Shitov
 
PDF
Good Evils In Perl
Kang-min Liu
 
PDF
Descobrindo a linguagem Perl
garux
 
PDF
Mojolicious. Веб в коробке!
Anatoly Sharifulin
 
PDF
Web 8 | Introduction to PHP
Mohammad Imam Hossain
 
PDF
07 Introduction to PHP #burningkeyboards
Denis Ristic
 
PDF
2014 database - course 2 - php
Hung-yu Lin
 
PDF
PHP 5.3 Overview
jsmith92
 
PDF
[PL] Jak nie zostać "programistą" PHP?
Radek Benkel
 
ODP
Modern Perl
Marcos Rebelo
 
KEY
Perl Web Client
Flavio Poletti
 
PDF
Generating Power with Yield
Jason Myers
 
ODP
Programming in perl style
Bo Hua Yang
 
KEY
GettingStartedWithPHP
Nat Weerawan
 
PDF
Blog Hacks 2011
Yusuke Wada
 
PDF
R57shell
ady36
 
PPTX
Perl basics for Pentesters
Sanjeev Kumar Jaiswal
 
PDF
PHPSpec BDD Framework
Marcello Duarte
 
PDF
Introduction to PHP
Bradley Holt
 
PPT
PHP Unit Testing
Tagged Social
 
Perl6 in-production
Andrew Shitov
 
Good Evils In Perl
Kang-min Liu
 
Descobrindo a linguagem Perl
garux
 
Mojolicious. Веб в коробке!
Anatoly Sharifulin
 
Web 8 | Introduction to PHP
Mohammad Imam Hossain
 
07 Introduction to PHP #burningkeyboards
Denis Ristic
 
2014 database - course 2 - php
Hung-yu Lin
 
PHP 5.3 Overview
jsmith92
 
[PL] Jak nie zostać "programistą" PHP?
Radek Benkel
 
Modern Perl
Marcos Rebelo
 
Perl Web Client
Flavio Poletti
 
Generating Power with Yield
Jason Myers
 
Programming in perl style
Bo Hua Yang
 
GettingStartedWithPHP
Nat Weerawan
 
Blog Hacks 2011
Yusuke Wada
 
R57shell
ady36
 
Perl basics for Pentesters
Sanjeev Kumar Jaiswal
 
PHPSpec BDD Framework
Marcello Duarte
 
Introduction to PHP
Bradley Holt
 
PHP Unit Testing
Tagged Social
 
Ad

More from brian d foy (15)

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

Recently uploaded (20)

PPTX
Top Managed Service Providers in Los Angeles
Captain IT
 
PDF
HR agent at Mediq: Lessons learned on Agent Builder & Maestro by Tacstone Tec...
UiPathCommunity
 
PDF
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
PDF
Integrating IIoT with SCADA in Oil & Gas A Technical Perspective.pdf
Rejig Digital
 
PDF
Rethinking Security Operations - Modern SOC.pdf
Haris Chughtai
 
PDF
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
PDF
visibel.ai Company Profile – Real-Time AI Solution for CCTV
visibelaiproject
 
PDF
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
PDF
OpenInfra ID 2025 - Are Containers Dying? Rethinking Isolation with MicroVMs.pdf
Muhammad Yuga Nugraha
 
PDF
UiPath on Tour London Community Booth Deck
UiPathCommunity
 
PPTX
python advanced data structure dictionary with examples python advanced data ...
sprasanna11
 
PPTX
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
PPTX
Simplifying End-to-End Apache CloudStack Deployment with a Web-Based Automati...
ShapeBlue
 
PDF
How Current Advanced Cyber Threats Transform Business Operation
Eryk Budi Pratama
 
PDF
Trading Volume Explained by CIFDAQ- Secret Of Market Trends
CIFDAQ
 
PDF
2025-07-15 EMEA Volledig Inzicht Dutch Webinar
ThousandEyes
 
PDF
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
PDF
Novus Safe Lite- What is Novus Safe Lite.pdf
Novus Hi-Tech
 
PDF
Novus-Safe Pro: Brochure-What is Novus Safe Pro?.pdf
Novus Hi-Tech
 
PPTX
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
Top Managed Service Providers in Los Angeles
Captain IT
 
HR agent at Mediq: Lessons learned on Agent Builder & Maestro by Tacstone Tec...
UiPathCommunity
 
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
Integrating IIoT with SCADA in Oil & Gas A Technical Perspective.pdf
Rejig Digital
 
Rethinking Security Operations - Modern SOC.pdf
Haris Chughtai
 
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
visibel.ai Company Profile – Real-Time AI Solution for CCTV
visibelaiproject
 
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
OpenInfra ID 2025 - Are Containers Dying? Rethinking Isolation with MicroVMs.pdf
Muhammad Yuga Nugraha
 
UiPath on Tour London Community Booth Deck
UiPathCommunity
 
python advanced data structure dictionary with examples python advanced data ...
sprasanna11
 
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
Simplifying End-to-End Apache CloudStack Deployment with a Web-Based Automati...
ShapeBlue
 
How Current Advanced Cyber Threats Transform Business Operation
Eryk Budi Pratama
 
Trading Volume Explained by CIFDAQ- Secret Of Market Trends
CIFDAQ
 
2025-07-15 EMEA Volledig Inzicht Dutch Webinar
ThousandEyes
 
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
Novus Safe Lite- What is Novus Safe Lite.pdf
Novus Hi-Tech
 
Novus-Safe Pro: Brochure-What is Novus Safe Pro?.pdf
Novus Hi-Tech
 
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 

Advanced modulinos