Advanced modulinos trial
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Advanced modulinos trial

  • 2,180 views
Uploaded on

This is a first try at the Advanced Modulinos talk I'll give at this summer's conferences. It's still rough and I'm working out some stuff.

This is a first try at the Advanced Modulinos talk I'll give at this summer's conferences. It's still rough and I'm working out some stuff.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,180
On Slideshare
2,180
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
8
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Advanced Modulinos★ brian d foy The Perl Review Houston Perl Mongers
  • 2. Files that work asprograms and modules at the same time
  • 3. Unit testingCode reüseDistribution
  • 4. # hello.pluse v5.10;say Hello World!;
  • 5. % perl hello.plHello World!
  • 6. use v5.10;run();sub run { say Hello World!; }
  • 7. % perl hello.plHello World!
  • 8. # Hello.pmuse v5.10;run() unless caller;sub run { say Hello World!; }
  • 9. % perl hello.plHello World!% perl -MHello -e 1%
  • 10. package Hello;use v5.10;__PACKAGE__->run unless caller;sub run { say Hello World!; }
  • 11. use Test::More;use Test::Output;use_ok( Hello );stdout_ok( sub { Hello->run() }, Hello World!, ... );
  • 12. package Hello;use v5.10;__PACKAGE__->run unless caller;sub run { my( $self ) = @_; say { $self->fh } Hello World!; }
  • 13. package Hello;use v5.10;...;sub fh { *STDOUT }
  • 14. use Test::More;use_ok( Hello );our $string;{open my $fh, >, $string;*Hello::fh = sub { $fh };}Hello->run;is($string, Hello World!);
  • 15. package Hello;use v5.10;...;BEGIN {my $fh = *STDOUT;sub fh { $fh }sub set_fh { $fh = ...;}
  • 16. use Test::More;use_ok( Hello );open my $fh, >, my $string;Hello->set_fh( $fh );Hello->run;is($string, Hello World!);
  • 17. % perl hello.plHello World!% perl hello.pl HoustonHello World!% perl hello.pl -m TexasHello World!% perl hello.pl < partiersHello World!
  • 18. 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;
  • 19. sub run { my( $class, @args ) = @_; my %args = $class->process_args(@args); my $self = $class->new(%args); say { $self->fh } $self->message; }
  • 20. 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; }
  • 21. $app->new( input_fh => $in, output_fh => $out, message => Hello World! );
  • 22. sub new { my( $class, %args ) = @_; my $self = bless {}, $class; foreach ( keys %args ) { # maybe more complicated $self->set( $_, $args{$_} ); } return $self; }