Slide 1

Slide 1 text

Sub::Exporter Crafting Custom Interfaces

Slide 2

Slide 2 text

rjbs @cpan.org Ricardo SIGNES

Slide 3

Slide 3 text

What’s an Exporter? •Something that takes care of the annoying details of importing.

Slide 4

Slide 4 text

What is Importing?

Slide 5

Slide 5 text

What is Importing? •Build it over there, then bring it here.

Slide 6

Slide 6 text

What is Importing? •Build it over there, then bring it here. •For our purposes, “it” is code.

Slide 7

Slide 7 text

Why Do We Import?

Slide 8

Slide 8 text

Why Do We Import? •We want someone else to do the hard, boring work.

Slide 9

Slide 9 text

Why Do We Import? •We want someone else to do the hard, boring work. •And we want it done cheap.

Slide 10

Slide 10 text

sub strftime { my($pkg,$fmt,$time); ($pkg,$fmt,$time,$tzname) = @_; my $me = ref($pkg) ? $pkg : bless []; if(defined $tzname) { $tzname = uc $tzname; $tzname = sprintf(“%+05d”,$tzname) unless($tzname =~ /\D/); $epoch = timegm(@{$time}[0..5]); @$me = gmtime($epoch + tz_offset($tzname) - tz_offset()); } else { @$me = @$time; undef $epoch; } _subs($me,$fmt); }

Slide 11

Slide 11 text

Date::Format::strftime

Slide 12

Slide 12 text

strftime

Slide 13

Slide 13 text

How Importing Works

Slide 14

Slide 14 text

How Importing Works •the client use-s a module

Slide 15

Slide 15 text

How Importing Works •the client use-s a module •the module’s import method is called

Slide 16

Slide 16 text

How Importing Works •the client use-s a module •the module’s import method is called •something ugly happens

Slide 17

Slide 17 text

How Importing Works •the client use-s a module •the module’s import method is called •something ugly happens •the client has more named subs

Slide 18

Slide 18 text

How Importing Works •usually that ugliness is Exporter.pm

Slide 19

Slide 19 text

How Importing Works •usually that ugliness is Exporter.pm # the dark and twisted heart of Exporter.pm *{“${callpkg}::$sym”} = \&{“${pkg}::$sym”};

Slide 20

Slide 20 text

*{"$::$"} = \&{"$::$"}; •the caller gets the same code •with the same name

Slide 21

Slide 21 text

*{"$::$"} = \&{"$::$"}; •Exporter.pm churns out identically named and constructed products.

Slide 22

Slide 22 text

The Factory Model

Slide 23

Slide 23 text

The Factory Model •One size fits all

Slide 24

Slide 24 text

The Factory Model •One size fits all •If it doesn’t fit your code, adjust your code.

Slide 25

Slide 25 text

The Factory Model •One size fits all •If it doesn’t fit your code, adjust your code. •Or abuse the Exporter

Slide 26

Slide 26 text

The Factory Model •There’s Only One Way To Do It

Slide 27

Slide 27 text

The Tool Metaphor

Slide 28

Slide 28 text

The Tool Metaphor •“You can’t write good code without good tools.”

Slide 29

Slide 29 text

The Tool Metaphor •“You can’t write good code without good tools.” •Exporters are tools for making tools.

Slide 30

Slide 30 text

The Tool Metaphor •“You can’t write good code without good tools.” •Exporters are tools for making tools. •Their quality has an impact all the way down the line.

Slide 31

Slide 31 text

Craftsman Tools

Slide 32

Slide 32 text

Craftsman Tools •We want adaptable tools, customized for our current needs.

Slide 33

Slide 33 text

Craftsman Tools •We want adaptable tools, customized for our current needs. •We want tools hand-crafted to our specifications.

Slide 34

Slide 34 text

Craftsman Tools •We want adaptable tools, customized for our current needs. •We want tools hand-crafted to our specifications. •We want to reduce our labor by having someone else do the boring work.

Slide 35

Slide 35 text

Sub::Exporter!

Slide 36

Slide 36 text

Basic Exporting

Slide 37

Slide 37 text

The Basics •String::Truncate •trunc: truncate a string to length •elide: truncate, ending in “...”

Slide 38

Slide 38 text

String::Truncate

Slide 39

Slide 39 text

String::Truncate $string = “This string is 34 characters long.”;

Slide 40

Slide 40 text

String::Truncate $string = “This string is 34 characters long.”; trunc($string, 10); # This strin

Slide 41

Slide 41 text

String::Truncate $string = “This string is 34 characters long.”; trunc($string, 10); # This strin elide($string, 10); # This st...

Slide 42

Slide 42 text

Basic Exports use String::Truncate qw(elide trunc); To let your client write:

Slide 43

Slide 43 text

Basic Exports package String::Truncate; use Sub::Exporter -setup => { exports => [ qw(elide trunc) ], };

Slide 44

Slide 44 text

Basic Groups use String::Truncate qw(:all) To let your client write:

Slide 45

Slide 45 text

Basic Groups package String::Truncate; use Sub::Exporter -setup => { exports => [ qw(elide trunc) ], };

Slide 46

Slide 46 text

Basic Groups package String::Truncate; use Sub::Exporter -setup => { exports => [ qw(elide trunc) ], groups => { all => [qw(elide trunc)] }, };

Slide 47

Slide 47 text

Basic Groups use String::Truncate qw(:basic) To let your client write:

Slide 48

Slide 48 text

Basic Groups package String::Truncate; use Sub::Exporter -setup => { exports => [ qw(elide trunc) ], groups => { basic => [qw(elide trunc)] } };

Slide 49

Slide 49 text

Basic Defaults use String::Truncate; # imports “trunc” To let your client write:

Slide 50

Slide 50 text

Basic Defaults package String::Truncate; use Sub::Exporter -setup => { exports => [ qw(elide trunc) ], groups => { basic => [ qw(elide trunc) ], default => [ qw(trunc) ] }, };

Slide 51

Slide 51 text

package String::Truncate; use Exporter; use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); @ISA = qw(Exporter); @EXPORT = qw(trunc); @EXPORT_OK = qw(elide trunc); %EXPORT_TAGS = ( all => \@EXPORT_OK, basic => [ qw(elide trunc) ], ); Using Exporter.pm

Slide 52

Slide 52 text

Renaming Exports

Slide 53

Slide 53 text

Renaming Exports •avoid namespace collisions

Slide 54

Slide 54 text

Renaming Exports use CGI qw(:standard); use LWP::Simple; my $head = head(...); # what happens?

Slide 55

Slide 55 text

Renaming Exports •avoid unclear or ambiguous names

Slide 56

Slide 56 text

Renaming Exports use File::Basename; use XML::Parser; my $file = fileparse($ARGV[0]); $parser->parsefile($file);

Slide 57

Slide 57 text

Renaming Exports •“privatize” subs imported to classes

Slide 58

Slide 58 text

Renaming Exports package XML::Parser::Hypothetical; use File::Basename;

Slide 59

Slide 59 text

Renaming Exports use Exporter::Renaming; use String::Truncate qw(elide), Renaming => [ trunc => ‘trunc_str’ ]; no Exporter::Renaming; Using Exporter::Renaming

Slide 60

Slide 60 text

Renaming Exports use String::Truncate qw(trunc), trunc => { -as => ‘trunc_str’ }; To let your client write:

Slide 61

Slide 61 text

Renaming Exports package String::Truncate; use Sub::Exporter -setup => { exports => [ qw(elide trunc) ], groups => { basic => [ qw(elide trunc) ], default => [ qw(trunc) ] }, };

Slide 62

Slide 62 text

Wait a second... use String::Truncate qw(trunc), trunc => { -as => ‘trunc_str’ };

Slide 63

Slide 63 text

Data::OptList Quick and Dirty Data Structures

Slide 64

Slide 64 text

Data::OptList @optlist = ( qw(alfa bravo), charlie => [ 0, 1, 2 ], delta => { a => 1 }, ‘echo’, foxtrox => undef, ‘gulf’, );

Slide 65

Slide 65 text

Data::OptList @optlist = ( qw(alfa bravo), charlie => [ 0, 1, 2 ], delta => { a => 1 }, ‘echo’, foxtrox => undef, ‘gulf’, ); $as_href = { alfa => undef, bravo => undef, charlie => [ 0, 1, 2], delta => { a => 1 }, echo => undef, foxtrot => undef, gulf => undef, ];

Slide 66

Slide 66 text

Data::OptList @optlist = ( qw(alfa bravo), charlie => [ 0, 1, 2 ], delta => { a => 1 }, ‘echo’, foxtrox => undef, ‘gulf’, ); $as_aref = [ [ alfa => undef ], [ bravo => undef ], [ charlie => [0,1,2] ], [ delta => {a => 1}], [ echo => undef ], [ foxtrot => undef ], [ gulf => undef ], ];

Slide 67

Slide 67 text

Data::OptList @optlist = ( qw(aye aye) love => [ qw(chex) ], love => [ qw(milk) ], aye => { sir => ‘!’ }, );

Slide 68

Slide 68 text

Data::OptList @optlist = ( qw(aye aye) love => [ qw(chex) ], love => [ qw(milk) ], aye => { sir => ‘!’ }, ); $as_aref = [ [ aye => undef ], [ aye => undef ], [ love => [qw(chex)] ], [ love => [qw(milk)] ], [ aye => {sir => ‘!’}] ];

Slide 69

Slide 69 text

Data::OptList $as_href = die “...”; @optlist = ( qw(aye aye) love => [ qw(chex) ], love => [ qw(milk) ], aye => { sir => ‘!’ }, );

Slide 70

Slide 70 text

Customizing Exports

Slide 71

Slide 71 text

Subclassed Exporters

Slide 72

Slide 72 text

Subclassed Exporters •import is a method

Slide 73

Slide 73 text

Subclassed Exporters •import is a method •that implies that exporters are classes

Slide 74

Slide 74 text

Subclassed Exporters •import is a method •that implies that exporters are classes •and that we can subclass them

Slide 75

Slide 75 text

package String::Truncate::Split; use base qw(String::Truncate); sub trunc { my ($string, $length) = @_; # ... return ($head, $tail); }

Slide 76

Slide 76 text

Subclassed Exporters

Slide 77

Slide 77 text

Subclassed Exporters • *{“$::$”} = \&{“$::$”};

Slide 78

Slide 78 text

Subclassed Exporters • *{“$::$”} = \&{“$::$”}; • @EXPORT has to be defined in the derived class

Slide 79

Slide 79 text

Subclassed Exporters • *{“$::$”} = \&{“$::$”}; • @EXPORT has to be defined in the derived class •the export has to be defined in the exporting package

Slide 80

Slide 80 text

package String::Truncate::Split; use base qw(String::Truncate); sub trunc { my ($string, $length) = @_; # ... return ($head, $tail); } 1;

Slide 81

Slide 81 text

package String::Truncate::Split; use base qw(String::Truncate); our @EXPORT_OK = @String::Truncate::EXPORT_OK; our @EXPORT = @String::Truncate::EXPORT; our %EXPORT_TAGS = %String::Truncate::EXPORT_TAGS; sub trunc { my ($string, $length) = @_; # ... return ($head, $tail); }

Slide 82

Slide 82 text

package String::Truncate::Split; use base qw(String::Truncate); our @EXPORT_OK = @String::Truncate::EXPORT_OK; our @EXPORT = @String::Truncate::EXPORT; our %EXPORT_TAGS = %String::Truncate::EXPORT_TAGS; sub trunc { my ($string, $length) = @_; # ... return ($head, $tail); } *$_ = \&{“String::Truncate::$_”} for grep { not defined &{__PACKAGE__.“::$_”} } @EXPORT;

Slide 83

Slide 83 text

package String::Truncate::Split; use base qw(String::Truncate); our @EXPORT_OK = @String::Truncate::EXPORT_OK; our @EXPORT = @String::Truncate::EXPORT; our %EXPORT_TAGS = %String::Truncate::EXPORT_TAGS; sub trunc { my ($string, $length) = @_; # ... return ($head, $tail); } do { no strict ‘refs’; *$_ = \&{“String::Truncate::$_”} for grep { not defined &{__PACKAGE__.“::$_”} } @EXPORT; }

Slide 84

Slide 84 text

Subclassed Exporters

Slide 85

Slide 85 text

Subclassed Exporters •Sub::Exporter finds exports with “can”

Slide 86

Slide 86 text

Subclassed Exporters •Sub::Exporter finds exports with “can” •this means you can subclass exporting toolkits, replacing just pieces

Slide 87

Slide 87 text

package String::Truncate::Split; use base qw(String::Truncate); sub trunc { my ($string, $length) = @_; # ... return ($head, $tail); }

Slide 88

Slide 88 text

Customizing Exports

Slide 89

Slide 89 text

Customizing Exports •What if you want trunc to work differently when you use it?

Slide 90

Slide 90 text

Customizing Exports •Some modules do this with package variables.

Slide 91

Slide 91 text

Package-Level Config use String::Truncate qw(:all); $String::Truncate::DEFAULT_LENGTH = 20; $String::Truncate::DEFAULT_MARKER = “--”;

Slide 92

Slide 92 text

Package-Level Config use String::Truncate qw(:all); $String::Truncate::DEFAULT_LENGTH = 20; $String::Truncate::DEFAULT_MARKER = “--”; use Tools::Useful;

Slide 93

Slide 93 text

Package-Level Config use String::Truncate (); use Tools::Useful; sub trunc { my ($string, $length) = @_; $length //= 20; String::Truncate::trunc($string, $length) }

Slide 94

Slide 94 text

Package-Level Config use String::Truncate (); use Tools::Useful; sub trunc { local $String::Truncate::DEFAULT_LENGTH = 20; String::Truncate::trunc(@_); }

Slide 95

Slide 95 text

Custom Imports use String::Truncate qw(trunc), elide => { -as => ‘trail_off’, marker => ‘etc’, };

Slide 96

Slide 96 text

Custom Imports use String::Truncate qw(trunc elide), elide => { -as => ‘trail_off’, marker => ‘etc’, };

Slide 97

Slide 97 text

Custom Imports use String::Truncate trunc => { -as => ‘trunc_str’, length => 10 }, elide => { -as => ‘elide_str’, length => 10 };

Slide 98

Slide 98 text

Custom Imports use String::Truncate -all => { -suffix => ‘_str’, length => 10 };

Slide 99

Slide 99 text

Exports to Order package String::Truncate; use Sub::Exporter -setup => { exports => [ qw(elide trunc) ], groups => { basic => [ qw(elide trunc) ], default => [ qw(trunc) ] }, };

Slide 100

Slide 100 text

Exports to Order package String::Truncate; use Sub::Exporter -setup => { exports => [ qw(elide trunc) ], groups => { basic => [ qw(elide trunc) ], default => [ qw(trunc) ] }, };

Slide 101

Slide 101 text

Exports to Order package String::Truncate; use Sub::Exporter -setup => { exports => [ elide => undef, trunc => undef, ], groups => { basic => [ qw(elide trunc) ], default => [ qw(trunc) ] }, };

Slide 102

Slide 102 text

Exports to Order package String::Truncate; use Sub::Exporter -setup => { exports => [ elide => \’_build_elide’, trunc => \’_build_trunc’, ], groups => { basic => [ qw(elide trunc) ], default => [ qw(trunc) ] }, };

Slide 103

Slide 103 text

Generating Routines

Slide 104

Slide 104 text

Generating Routines sub _build_trunc {

Slide 105

Slide 105 text

Generating Routines sub _build_trunc { my ($class, $name, $arg) = @_;

Slide 106

Slide 106 text

Generating Routines sub _build_trunc { my ($class, $name, $arg) = @_; my $_length = $arg->{length};

Slide 107

Slide 107 text

Generating Routines sub _build_trunc { my ($class, $name, $arg) = @_; my $_length = $arg->{length}; return sub {

Slide 108

Slide 108 text

Generating Routines sub _build_trunc { my ($class, $name, $arg) = @_; my $_length = $arg->{length}; return sub { my ($string, $length, @rest) = @_;

Slide 109

Slide 109 text

Generating Routines sub _build_trunc { my ($class, $name, $arg) = @_; my $_length = $arg->{length}; return sub { my ($string, $length, @rest) = @_; $length //= $_length;

Slide 110

Slide 110 text

Generating Routines sub _build_trunc { my ($class, $name, $arg) = @_; my $_length = $arg->{length}; return sub { my ($string, $length, @rest) = @_; $length //= $_length; trunc($string, $length, @rest);

Slide 111

Slide 111 text

Generating Routines sub _build_trunc { my ($class, $name, $arg) = @_; my $_length = $arg->{length}; return sub { my ($string, $length, @rest) = @_; $length //= $_length; trunc($string, $length, @rest); }

Slide 112

Slide 112 text

Generating Routines sub _build_trunc { my ($class, $name, $arg) = @_; my $_length = $arg->{length}; return sub { my ($string, $length, @rest) = @_; $length //= $_length; trunc($string, $length, @rest); } }

Slide 113

Slide 113 text

Routines ex nihilo

Slide 114

Slide 114 text

Routines ex nihilo use Cypher::Trivial qw(cyphers);

Slide 115

Slide 115 text

Routines ex nihilo use Cypher::Trivial qw(cyphers); my ($encyph, $decyph) = cyphers(“secret”);

Slide 116

Slide 116 text

Routines ex nihilo use Cypher::Trivial qw(cyphers); my ($encyph, $decyph) = cyphers(“secret”); $cyphertext

Slide 117

Slide 117 text

Routines ex nihilo use Cypher::Trivial qw(cyphers); my ($encyph, $decyph) = cyphers(“secret”); $cyphertext = $encyph->(“Top secret message.”);

Slide 118

Slide 118 text

Routines ex nihilo use Cypher::Trivial qw(cyphers); my ($encyph, $decyph) = cyphers(“secret”); $cyphertext = $encyph->(“Top secret message.”); sub encypher {

Slide 119

Slide 119 text

Routines ex nihilo use Cypher::Trivial qw(cyphers); my ($encyph, $decyph) = cyphers(“secret”); $cyphertext = $encyph->(“Top secret message.”); sub encypher { my $text = shift; $encyph->($text);

Slide 120

Slide 120 text

Routines ex nihilo use Cypher::Trivial qw(cyphers); my ($encyph, $decyph) = cyphers(“secret”); $cyphertext = $encyph->(“Top secret message.”); sub encypher { my $text = shift; $encyph->($text); }

Slide 121

Slide 121 text

Routines ex nihilo

Slide 122

Slide 122 text

Routines ex nihilo use Cypher::Trivial

Slide 123

Slide 123 text

Routines ex nihilo use Cypher::Trivial encypher => { secret => “secret” };

Slide 124

Slide 124 text

Routines ex nihilo use Cypher::Trivial encypher => { secret => “secret” }; encypher(“Top secret message”);

Slide 125

Slide 125 text

Routines ex nihilo sub _build_encypher { my ($class, $name, $arg) = @_; my ($enc, $dec) = cyphers($arg->{secret}); return $enc; } sub _build_decypher { my ($class, $name, $arg) = @_; my ($enc, $dec) = cyphers($arg->{secret}); return $dec; }

Slide 126

Slide 126 text

Routines ex nihilo sub _build_encypher { my ($class, $name, $arg) = @_; my ($enc, $dec) = cyphers($arg->{secret}); return $enc; } sub _build_decypher { my ($class, $name, $arg) = @_; my ($enc, $dec) = cyphers($arg->{secret}); return $dec; }

Slide 127

Slide 127 text

Routines ex nihilo package Cypher::Trivial; use Sub::Exporter -setup => { exports => [ encypher => \’_build_encypher’, decypher => \’_build_decypher’, cyphers => undef, ], groups => { cyphers => [ qw(encypher decypher) ], } };

Slide 128

Slide 128 text

Routines ex nihilo use Cypher::Trivial encypher => { secret => “secret” }; encypher(“Top secret message”);

Slide 129

Slide 129 text

Routines ex nihilo use Cypher::Trivial -cyphers => { secret => “secret” }; encypher(“Top secret message”); decypher(“Gbc frperg zrffntr”);

Slide 130

Slide 130 text

Generating Groups package Cypher::Trivial; use Sub::Exporter -setup => { exports => [ encypher => \’_build_encypher’, decypher => \’_build_decypher’, cyphers => undef, ], groups => { cyphers => [ qw(encypher decypher) ], } };

Slide 131

Slide 131 text

Generating Groups sub _build_encypher { my ($class, $name, $arg) = @_; my ($enc, $dec) = cyphers($arg->{secret}); return $enc; } sub _build_decypher { my ($class, $name, $arg) = @_; my ($enc, $dec) = cyphers($arg->{secret}); return $dec; }

Slide 132

Slide 132 text

Generating Groups package Cypher::Trivial; use Sub::Exporter -setup => { exports => [ encypher => \’_build_encypher’, decypher => \’_build_decypher’, cyphers => undef, ], groups => { cyphers => \’_build_cyphers’, } };

Slide 133

Slide 133 text

Generating Groups

Slide 134

Slide 134 text

Generating Groups sub _build_cyphers {

Slide 135

Slide 135 text

Generating Groups sub _build_cyphers { my ($class, $name, $arg) = @_;

Slide 136

Slide 136 text

Generating Groups sub _build_cyphers { my ($class, $name, $arg) = @_; my ($enc, $dec) = cyphers($arg->{secret});

Slide 137

Slide 137 text

Generating Groups sub _build_cyphers { my ($class, $name, $arg) = @_; my ($enc, $dec) = cyphers($arg->{secret}); return {

Slide 138

Slide 138 text

Generating Groups sub _build_cyphers { my ($class, $name, $arg) = @_; my ($enc, $dec) = cyphers($arg->{secret}); return { encypher => $enc,

Slide 139

Slide 139 text

Generating Groups sub _build_cyphers { my ($class, $name, $arg) = @_; my ($enc, $dec) = cyphers($arg->{secret}); return { encypher => $enc, decypher => $dec,

Slide 140

Slide 140 text

Generating Groups sub _build_cyphers { my ($class, $name, $arg) = @_; my ($enc, $dec) = cyphers($arg->{secret}); return { encypher => $enc, decypher => $dec, };

Slide 141

Slide 141 text

Generating Groups sub _build_cyphers { my ($class, $name, $arg) = @_; my ($enc, $dec) = cyphers($arg->{secret}); return { encypher => $enc, decypher => $dec, }; }

Slide 142

Slide 142 text

Generating Groups use Cypher::Trivial -cyphers => { secret => “secret” };

Slide 143

Slide 143 text

Generating Groups use Cypher::Trivial -cyphers => { secret => “secret” }, -cyphers => { secret => ‘Secret1234’, -suffix => ‘_strong’ } ;

Slide 144

Slide 144 text

Exporting Methods

Slide 145

Slide 145 text

ZOMG O NO!

Slide 146

Slide 146 text

Methods & Exporter.pm

Slide 147

Slide 147 text

Methods & Exporter.pm •Exporter.pm: “Do not export method names!”

Slide 148

Slide 148 text

Methods & Exporter.pm •Exporter.pm: “Do not export method names!” • *{“$::$”} = \&{“$::$”};

Slide 149

Slide 149 text

package Object::Hybrid; use Exporter; @Object::Exporter::ISA = qw(Exporter); @Object::Exporter::EXPORT_OK = qw(retrieve); sub retrieve { my ($class, $id) = @_; my $row = $class->get_row(id => $id); bless $row => $class; } Methods & Exporter.pm

Slide 150

Slide 150 text

use Object::Hybrid qw(retrieve); my $object = retrieve(42); my $object = retrieve(49); Methods & Exporter.pm

Slide 151

Slide 151 text

package Object::Hybrid; use Exporter; @Object::Exporter::ISA = qw(Exporter); @Object::Exporter::EXPORT_OK = qw(object); sub retrieve { my ($class, $id) = @_; my $row = $class->get_row(id => $id); bless $row => $class; } sub object { __PACKAGE__->retrieve(@_) } Methods & Exporter.pm

Slide 152

Slide 152 text

use Object::Hybrid qw(object); my $object = object(42); my $object = object(49); Methods & Exporter.pm

Slide 153

Slide 153 text

use Object::Hybrid; my $object = Object::Hybrid->object(42); Methods & Exporter.pm

Slide 154

Slide 154 text

Methods & Exporter.pm

Slide 155

Slide 155 text

use Object::Hybrid; Methods & Exporter.pm

Slide 156

Slide 156 text

use Object::Hybrid; use Object::Hybrid::With::Much::Derivation; Methods & Exporter.pm

Slide 157

Slide 157 text

use Object::Hybrid; use Object::Hybrid::With::Much::Derivation; my $object = Object::Hybrid->retrieve(42); Methods & Exporter.pm

Slide 158

Slide 158 text

use Object::Hybrid; use Object::Hybrid::With::Much::Derivation; my $object = Object::Hybrid->retrieve(42); my $thing = Methods & Exporter.pm

Slide 159

Slide 159 text

use Object::Hybrid; use Object::Hybrid::With::Much::Derivation; my $object = Object::Hybrid->retrieve(42); my $thing = Object::Hybrid::With::Much::Derivation Methods & Exporter.pm

Slide 160

Slide 160 text

use Object::Hybrid; use Object::Hybrid::With::Much::Derivation; my $object = Object::Hybrid->retrieve(42); my $thing = Object::Hybrid::With::Much::Derivation ->retrieve(49); Methods & Exporter.pm

Slide 161

Slide 161 text

package Object::Hybrid; use Exporter; @Object::Exporter::ISA = qw(Exporter); @Object::Exporter::EXPORT_OK = qw(object); sub retrieve { my ($class, $id) = @_; my $row = $class->get_row(id => $id); bless $row => $class; } sub object { __PACKAGE__->retrieve(@_) } Methods & Exporter.pm

Slide 162

Slide 162 text

Currying Methods use Object::Hybrid qw(object); use Object::Hybrid::With::Much::Derivation object => { -as => ‘derived_object’ }; my $object = object(42); my $thing = derived_object(49);

Slide 163

Slide 163 text

Currying Methods use Object::Hybrid qw(object); my $object = Object::Hybrid->object(49);

Slide 164

Slide 164 text

Currying Methods

Slide 165

Slide 165 text

Currying Methods

Slide 166

Slide 166 text

Currying Methods use Sub::Exporter -setup => {

Slide 167

Slide 167 text

Currying Methods use Sub::Exporter -setup => { exports => [ object => \&_build_object ],

Slide 168

Slide 168 text

Currying Methods use Sub::Exporter -setup => { exports => [ object => \&_build_object ], };

Slide 169

Slide 169 text

Currying Methods use Sub::Exporter -setup => { exports => [ object => \&_build_object ], }; sub _build_object {

Slide 170

Slide 170 text

Currying Methods use Sub::Exporter -setup => { exports => [ object => \&_build_object ], }; sub _build_object { my ($class, $name, $arg) = @_;

Slide 171

Slide 171 text

Currying Methods use Sub::Exporter -setup => { exports => [ object => \&_build_object ], }; sub _build_object { my ($class, $name, $arg) = @_; return sub { $class->new(@_); }

Slide 172

Slide 172 text

Currying Methods use Sub::Exporter -setup => { exports => [ object => \&_build_object ], }; sub _build_object { my ($class, $name, $arg) = @_; return sub { $class->new(@_); } }

Slide 173

Slide 173 text

Currying Methods use Sub::Exporter -setup => { exports => [ object => curry_class(‘new’) ], }

Slide 174

Slide 174 text

Currying Methods use Sub::Exporter::Util qw(curry_class); use Sub::Exporter -setup => { exports => [ object => curry_class(‘new’) ], }

Slide 175

Slide 175 text

Exporting Methods

Slide 176

Slide 176 text

Exporting Methods •Sometimes you want to export methods without currying the class.

Slide 177

Slide 177 text

Exporting Methods •Sometimes you want to export methods without currying the class. •Exporters can serve as method crafters.

Slide 178

Slide 178 text

Exporting Methods package Mixin::Dumper; use Sub::Exporter -setup => { exports => [ qw(dump) ], groups => { default => [ qw(dump) ] }, }; sub dump { my ($self) = @_; require Data::Dumper; Data::Dumper::Dumper($self); }

Slide 179

Slide 179 text

Exporting Methods package Email::Simple::mixin:ReplyText; use Sub::Exporter -setup => { exports => [ qw(reply_text) ], groups => { defaults => [ qw(reply_text) ] }, }; sub reply_text { my ($self) = @_; join “\n”, map “>$_”, split /\n/, $self->body; }

Slide 180

Slide 180 text

Exporting Methods package Email::Simple::mixin:ReplyText; use Sub::Exporter -setup => { into => ‘Email::Simple’, exports => [ qw(reply_text) ], groups => { defaults => [ qw(reply_text) ] }, }; sub reply_text { my ($self) = @_; join “\n”, map “>$_”, split /\n/, $self->body; }

Slide 181

Slide 181 text

Exporting Methods use Email::Simple; use Email::Simple::mixin::ReplyText;

Slide 182

Slide 182 text

Exporting Methods use Email::Simple; use Email::Simple::mixin::ReplyText; use Email::Simple::Mock; use Email::Simple::mixin::ReplyText { into => ‘Email::Simple::Mock’ };

Slide 183

Slide 183 text

Emulating mixin.pm

Slide 184

Slide 184 text

Emulating mixin.pm •Don’t import into my namespace...

Slide 185

Slide 185 text

Emulating mixin.pm •Don’t import into my namespace... •...import to a new namespace...

Slide 186

Slide 186 text

Emulating mixin.pm •Don’t import into my namespace... •...import to a new namespace... •...and add it to my @ISA.

Slide 187

Slide 187 text

Emulating mixin.pm

Slide 188

Slide 188 text

Emulating mixin.pm •This makes it easy to import a chunk of methods and override just a few...

Slide 189

Slide 189 text

Emulating mixin.pm •This makes it easy to import a chunk of methods and override just a few... •...and those few can call SUPER.

Slide 190

Slide 190 text

Emulating mixin.pm package Email::Simple::mixin:ReplyText; use Sub::Exporter -setup => { into => ‘Email::Simple’, exports => [ qw(reply_text) ], groups => { defaults => [ qw(reply_text) ] }, }; sub reply_text { my ($self) = @_; join “\n”, map “>$_”, split /\n/, $self->body; }

Slide 191

Slide 191 text

Emulating mixin.pm package Email::Simple::mixin:ReplyText; use Sub::Exporter -setup => { into => ‘Email::Simple’, exporter=> mixin_exporter, exports => [ qw(reply_text) ], groups => { defaults => [ qw(reply_text) ] }, }; sub reply_text { my ($self) = @_; join “\n”, map “>$_”, split /\n/, $self->body; }

Slide 192

Slide 192 text

Collectors

Slide 193

Slide 193 text

Collectors

Slide 194

Slide 194 text

Collectors •Arguments that don’t export anything.

Slide 195

Slide 195 text

Collectors •Arguments that don’t export anything. •They collect data for generators to use.

Slide 196

Slide 196 text

Collectors package String::Truncate; use Sub::Exporter -setup => { exports => [ elide => \’_build_elide’, trunc => \’_build_trunc’, ], collectors => [ qw(defaults) ], };

Slide 197

Slide 197 text

Collectors

Slide 198

Slide 198 text

Collectors use String::Truncate

Slide 199

Slide 199 text

Collectors use String::Truncate defaults => { length => 10 },

Slide 200

Slide 200 text

Collectors use String::Truncate defaults => { length => 10 }, qw(-all),

Slide 201

Slide 201 text

Collectors use String::Truncate defaults => { length => 10 }, qw(-all), trunc => { length => 1, -as => ‘onechar’ },

Slide 202

Slide 202 text

Collectors use String::Truncate defaults => { length => 10 }, qw(-all), trunc => { length => 1, -as => ‘onechar’ }, elide => { marker => ‘&c’, -as => ‘yul’ },

Slide 203

Slide 203 text

Collectors use String::Truncate defaults => { length => 10 }, qw(-all), trunc => { length => 1, -as => ‘onechar’ }, elide => { marker => ‘&c’, -as => ‘yul’ }, ;

Slide 204

Slide 204 text

Collectors sub _build_trunc { my ($class, $name, $arg) = @_; my $_length = $arg->{length}; return sub { my ($string, $length, @rest) = @_; $length = $_length if !defined $length; trunc($string, $length, @rest); } }

Slide 205

Slide 205 text

Collectors sub _build_trunc { my ($class, $name, $arg, $col) = @_; my $_length = $arg->{length}; return sub { my ($string, $length, @rest) = @_; $length = $_length if !defined $length; trunc($string, $length, @rest); } }

Slide 206

Slide 206 text

Collectors sub _build_trunc { my ($class, $name, $arg, $col) = @_; my $_length = $arg->{length}; $_length = $col->{defaults}{length} if !defined $_length; return sub { my ($string, $length, @rest) = @_; $length = $_length if !defined $length; trunc($string, $length, @rest); } }

Slide 207

Slide 207 text

Collectors

Slide 208

Slide 208 text

Collectors •Arguments that don’t export. •They collect data for generators to use.

Slide 209

Slide 209 text

Collectors •Arguments that don’t export. •They collect data for generators to use. •They can validate the collected data.

Slide 210

Slide 210 text

Collectors package String::Truncate; use Sub::Exporter -setup => { exports => [ elide => \’_build_elide’, trunc => \’_build_trunc’, ], collectors => { defaults => \’_validate_defaults’, }, };

Slide 211

Slide 211 text

Collectors sub _validate_defaults { my ($class, $value, $data) = @_; return (ref $value eq ‘HASH’); }

Slide 212

Slide 212 text

Collectors

Slide 213

Slide 213 text

Collectors •Arguments that don’t export. •They collect data for generators to use. •They can validate the collected data.

Slide 214

Slide 214 text

Collectors •Arguments that don’t export. •They collect data for generators to use. •They can validate the collected data. •They can do Almost Anything Else.

Slide 215

Slide 215 text

Collectors sub _validate_defaults { my ($class, $value, $data) = @_; return (ref $value eq ‘HASH’); }

Slide 216

Slide 216 text

Collectors

Slide 217

Slide 217 text

Collectors •name - name of the collection

Slide 218

Slide 218 text

Collectors •name - name of the collection •class - invocant of import method

Slide 219

Slide 219 text

Collectors •name - name of the collection •class - invocant of import method •config - exporter configuration

Slide 220

Slide 220 text

Collectors •name - name of the collection •class - invocant of import method •config - exporter configuration •into - the package that’s importing

Slide 221

Slide 221 text

Collectors •name - name of the collection •class - invocant of import method •config - exporter configuration •into - the package that’s importing •import_args - args to import method

Slide 222

Slide 222 text

Collectors

Slide 223

Slide 223 text

Collectors •name - the name of the collection

Slide 224

Slide 224 text

Collectors •name - the name of the collection •class - import’s invocant

Slide 225

Slide 225 text

Collectors

Slide 226

Slide 226 text

Collectors •config - the Sub::Exporter config

Slide 227

Slide 227 text

Collectors •config - the Sub::Exporter config •find out what exports exist

Slide 228

Slide 228 text

Collectors •config - the Sub::Exporter config •find out what exports exist •validate collection value based on config

Slide 229

Slide 229 text

use LWP::Simple “/^is_/”; is_success($res); is_failure($res);

Slide 230

Slide 230 text

use LWP::Simpleton; use Sub::Exporter -setup => { collectors => { like => Sub::Exporter::Util::like }, };

Slide 231

Slide 231 text

use LWP::Simple like => qr/^is_/; is_success($res); is_failure($res);

Slide 232

Slide 232 text

use LWP::Simple like => [ qr/^is_/, undef, qr/^get/, { -prefix => ‘https_’, ssl => 1 } ]; is_success($res); is_failure($res); https_get(“https://codesimply.com”)

Slide 233

Slide 233 text

Collectors

Slide 234

Slide 234 text

Collectors •into - the target to which exports go

Slide 235

Slide 235 text

Collectors •into - the target to which exports go •alter the class directly

Slide 236

Slide 236 text

Collectors •into - the target to which exports go •alter the class directly •particularly useful: @ISA

Slide 237

Slide 237 text

No content

Slide 238

Slide 238 text

sub _make_base { my ($class, $value, $data) = @_; my $target = $data->{into}; push @{“$target\::ISA”}, $class; }

Slide 239

Slide 239 text

sub _make_base { my ($class, $value, $data) = @_; my $target = $data->{into}; push @{“$target\::ISA”}, $class; } use Sub::Exporter -setup => { collectors => { -base => \’_make_base’ }, };

Slide 240

Slide 240 text

sub _make_base { my ($class, $value, $data) = @_; my $target = $data->{into}; push @{“$target\::ISA”}, $class; } use Sub::Exporter -setup => { collectors => { -base => \’_make_base’ }, }; use Magic::Superclass -base;

Slide 241

Slide 241 text

No content

Slide 242

Slide 242 text

package Email::Constants; sub _set_constants { my ($class, $value, $data) = @_; Package::Generator->assign_symbols( $data->{into}, [ EX_TEMPFAIL => 75, FORMATS => [ qw(Maildir mbox mh) ], ], ); }

Slide 243

Slide 243 text

package Email::Constants; sub _set_constants { my ($class, $value, $data) = @_; Package::Generator->assign_symbols( $data->{into}, [ EX_TEMPFAIL => 75, FORMATS => [ qw(Maildir mbox mh) ], ], ); } use Sub::Exporter -setup => { collectors => { constants => \’_set_constants’ }, };

Slide 244

Slide 244 text

use Email::Constants qw(constants);

Slide 245

Slide 245 text

Collectors

Slide 246

Slide 246 text

Collectors •import_args - the arguments to import

Slide 247

Slide 247 text

Collectors •import_args - the arguments to import •rewrite the arguments list

Slide 248

Slide 248 text

Collectors •import_args - the arguments to import •rewrite the arguments list •add new imports

Slide 249

Slide 249 text

No content

Slide 250

Slide 250 text

sub _setup {

Slide 251

Slide 251 text

sub _setup { my ($class, $value, $data) = @_;

Slide 252

Slide 252 text

sub _setup { my ($class, $value, $data) = @_; if (ref $value eq ‘HASH’) {

Slide 253

Slide 253 text

sub _setup { my ($class, $value, $data) = @_; if (ref $value eq ‘HASH’) { push @{ $data->{import_args} },

Slide 254

Slide 254 text

sub _setup { my ($class, $value, $data) = @_; if (ref $value eq ‘HASH’) { push @{ $data->{import_args} }, [ _import => { -as => ‘import’, %$value } ];

Slide 255

Slide 255 text

sub _setup { my ($class, $value, $data) = @_; if (ref $value eq ‘HASH’) { push @{ $data->{import_args} }, [ _import => { -as => ‘import’, %$value } ]; return 1;

Slide 256

Slide 256 text

sub _setup { my ($class, $value, $data) = @_; if (ref $value eq ‘HASH’) { push @{ $data->{import_args} }, [ _import => { -as => ‘import’, %$value } ]; return 1; } elsif (ref $value eq ‘ARRAY’) {

Slide 257

Slide 257 text

sub _setup { my ($class, $value, $data) = @_; if (ref $value eq ‘HASH’) { push @{ $data->{import_args} }, [ _import => { -as => ‘import’, %$value } ]; return 1; } elsif (ref $value eq ‘ARRAY’) { push @{ $data->{import_args} },

Slide 258

Slide 258 text

sub _setup { my ($class, $value, $data) = @_; if (ref $value eq ‘HASH’) { push @{ $data->{import_args} }, [ _import => { -as => ‘import’, %$value } ]; return 1; } elsif (ref $value eq ‘ARRAY’) { push @{ $data->{import_args} }, [ _import => {

Slide 259

Slide 259 text

sub _setup { my ($class, $value, $data) = @_; if (ref $value eq ‘HASH’) { push @{ $data->{import_args} }, [ _import => { -as => ‘import’, %$value } ]; return 1; } elsif (ref $value eq ‘ARRAY’) { push @{ $data->{import_args} }, [ _import => { -as => ‘import’, exports => $value } ];

Slide 260

Slide 260 text

sub _setup { my ($class, $value, $data) = @_; if (ref $value eq ‘HASH’) { push @{ $data->{import_args} }, [ _import => { -as => ‘import’, %$value } ]; return 1; } elsif (ref $value eq ‘ARRAY’) { push @{ $data->{import_args} }, [ _import => { -as => ‘import’, exports => $value } ]; return 1;

Slide 261

Slide 261 text

sub _setup { my ($class, $value, $data) = @_; if (ref $value eq ‘HASH’) { push @{ $data->{import_args} }, [ _import => { -as => ‘import’, %$value } ]; return 1; } elsif (ref $value eq ‘ARRAY’) { push @{ $data->{import_args} }, [ _import => { -as => ‘import’, exports => $value } ]; return 1; }

Slide 262

Slide 262 text

sub _setup { my ($class, $value, $data) = @_; if (ref $value eq ‘HASH’) { push @{ $data->{import_args} }, [ _import => { -as => ‘import’, %$value } ]; return 1; } elsif (ref $value eq ‘ARRAY’) { push @{ $data->{import_args} }, [ _import => { -as => ‘import’, exports => $value } ]; return 1; } return;

Slide 263

Slide 263 text

sub _setup { my ($class, $value, $data) = @_; if (ref $value eq ‘HASH’) { push @{ $data->{import_args} }, [ _import => { -as => ‘import’, %$value } ]; return 1; } elsif (ref $value eq ‘ARRAY’) { push @{ $data->{import_args} }, [ _import => { -as => ‘import’, exports => $value } ]; return 1; } return; }

Slide 264

Slide 264 text

No content

Slide 265

Slide 265 text

use Sub::Exporter -setup => {

Slide 266

Slide 266 text

use Sub::Exporter -setup => { collectors => { -setup => \’_setup’ },

Slide 267

Slide 267 text

use Sub::Exporter -setup => { collectors => { -setup => \’_setup’ }, exports => [ _import => \’_build_import’ ],

Slide 268

Slide 268 text

use Sub::Exporter -setup => { collectors => { -setup => \’_setup’ }, exports => [ _import => \’_build_import’ ], });

Slide 269

Slide 269 text

-setup => { into_level => 2, exports => [qw(foo)] }

Slide 270

Slide 270 text

-setup => { into_level => 2, exports => [qw(foo)] } _import => { -as => ‘import’, into_level => 2, exports => [qw(foo)] }

Slide 271

Slide 271 text

-setup => [ qw(foo bar baz) ]

Slide 272

Slide 272 text

-setup => [ qw(foo bar baz) ] _import => { -as => ‘import’, exports => [qw(foo bar baz)] }

Slide 273

Slide 273 text

use Sub::Exporter -setup => { collectors => { -setup => \’_setup’ }, exports => [ _import => \’_build_import’ ], });

Slide 274

Slide 274 text

use Sub::Exporter -setup => { collectors => { -setup => \’_setup’ }, exports => [ _import => sub { my ($class, $name, $arg) = @_; build_exporter($arg); }, ], });

Slide 275

Slide 275 text

package Sub::Exporter; use Sub::Exporter -setup => { collectors => { -setup => \&_setup }, exports => [ _import => sub { my ($class, $name, $arg) = @_; build_exporter($arg); }, ], });

Slide 276

Slide 276 text

RJBS’s Advice

Slide 277

Slide 277 text

RJBS’s Advice •Write the client code first.

Slide 278

Slide 278 text

RJBS’s Advice •Write the client code first. •Make as many assumptions as possible.

Slide 279

Slide 279 text

RJBS’s Advice •Write the client code first. •Make as many assumptions as possible. •Let most of them be refuted.

Slide 280

Slide 280 text

Any Questions?

Slide 281

Slide 281 text

Random Tricks

Slide 282

Slide 282 text

Mixed-in Helpers $object->complex_method($arg);

Slide 283

Slide 283 text

Mixed-in Helpers sub _build_cplx_method { my ($mixin) = @_; sub { my ($self, $arg) = @_; $mixin->validate_arg($arg); $mixin->do_stuff($self, $arg); return $mixin->analyze($self); } } sub validate_arg {...}

Slide 284

Slide 284 text

Mixed-in Helpers package Mixin::Helper; use Sub::Exporter -setup => { exports => [ complex_method => \’_build_cplx_method’, ], }; sub _build_cplx_method { ...

Slide 285

Slide 285 text

Mixed-in Helpers sub _build_cplx_method { my ($mixin) = @_; sub { my ($self, $arg) = @_; $mixin->validate_arg($arg); $mixin->do_stuff($self, $arg); return $mixin->analyze($self); } } sub validate_arg {...}

Slide 286

Slide 286 text

Mixed-in Helpers package Mixin::Helper::Faster; use base qw(Mixin::Helper); sub analyze { my ($mixin, $object) = @_; return 1; } 1;

Slide 287

Slide 287 text

A Coderef Generator

Slide 288

Slide 288 text

A Coderef Generator use String::Truncate ();

Slide 289

Slide 289 text

A Coderef Generator use String::Truncate (); my $trunc;

Slide 290

Slide 290 text

A Coderef Generator use String::Truncate (); my $trunc; String::Truncate->import(

Slide 291

Slide 291 text

A Coderef Generator use String::Truncate (); my $trunc; String::Truncate->import(trunc =>

Slide 292

Slide 292 text

A Coderef Generator use String::Truncate (); my $trunc; String::Truncate->import(trunc => { -as => \$trunc });

Slide 293

Slide 293 text

package YAPC::Slideshow; use Accessors::Simple -setup => { fields => [ qw(topic presenter timeslot room) ], }; Accessors sans ISA

Slide 294

Slide 294 text

Accessors sans ISA

Slide 295

Slide 295 text

sub _make_accessor { Accessors sans ISA

Slide 296

Slide 296 text

sub _make_accessor { my ($field) = @_; Accessors sans ISA

Slide 297

Slide 297 text

sub _make_accessor { my ($field) = @_; sub { Accessors sans ISA

Slide 298

Slide 298 text

sub _make_accessor { my ($field) = @_; sub { my ($self) = shift; Accessors sans ISA

Slide 299

Slide 299 text

sub _make_accessor { my ($field) = @_; sub { my ($self) = shift; $self->{field} = shift if @_; Accessors sans ISA

Slide 300

Slide 300 text

sub _make_accessor { my ($field) = @_; sub { my ($self) = shift; $self->{field} = shift if @_; return $self->{$field}; Accessors sans ISA

Slide 301

Slide 301 text

sub _make_accessor { my ($field) = @_; sub { my ($self) = shift; $self->{field} = shift if @_; return $self->{$field}; } Accessors sans ISA

Slide 302

Slide 302 text

sub _make_accessor { my ($field) = @_; sub { my ($self) = shift; $self->{field} = shift if @_; return $self->{$field}; } } Accessors sans ISA

Slide 303

Slide 303 text

sub _make_accessor { my ($field) = @_; sub { my ($self) = shift; $self->{field} = shift if @_; return $self->{$field}; } } sub _make_many_accessors { Accessors sans ISA

Slide 304

Slide 304 text

sub _make_accessor { my ($field) = @_; sub { my ($self) = shift; $self->{field} = shift if @_; return $self->{$field}; } } sub _make_many_accessors { my @fields = @{ $arg->{fields} }; Accessors sans ISA

Slide 305

Slide 305 text

sub _make_accessor { my ($field) = @_; sub { my ($self) = shift; $self->{field} = shift if @_; return $self->{$field}; } } sub _make_many_accessors { my @fields = @{ $arg->{fields} }; my %sub = map { $_ => _make_accessor($_) } @fields; Accessors sans ISA

Slide 306

Slide 306 text

sub _make_accessor { my ($field) = @_; sub { my ($self) = shift; $self->{field} = shift if @_; return $self->{$field}; } } sub _make_many_accessors { my @fields = @{ $arg->{fields} }; my %sub = map { $_ => _make_accessor($_) } @fields; return \%sub; Accessors sans ISA

Slide 307

Slide 307 text

sub _make_accessor { my ($field) = @_; sub { my ($self) = shift; $self->{field} = shift if @_; return $self->{$field}; } } sub _make_many_accessors { my @fields = @{ $arg->{fields} }; my %sub = map { $_ => _make_accessor($_) } @fields; return \%sub; } Accessors sans ISA

Slide 308

Slide 308 text

sub _make_accessor { my ($field) = @_; sub { my ($self) = shift; $self->{field} = shift if @_; return $self->{$field}; } } sub _make_many_accessors { my @fields = @{ $arg->{fields} }; my %sub = map { $_ => _make_accessor($_) } @fields; return \%sub; } use Sub::Exporter -setup => Accessors sans ISA

Slide 309

Slide 309 text

sub _make_accessor { my ($field) = @_; sub { my ($self) = shift; $self->{field} = shift if @_; return $self->{$field}; } } sub _make_many_accessors { my @fields = @{ $arg->{fields} }; my %sub = map { $_ => _make_accessor($_) } @fields; return \%sub; } use Sub::Exporter -setup => { groups => { setup => \&_make_many_accessors } }; Accessors sans ISA

Slide 310

Slide 310 text

Eat Exporter’s Brain

Slide 311

Slide 311 text

sub exporter_upgrade { Eat Exporter’s Brain

Slide 312

Slide 312 text

sub exporter_upgrade { my ($pkg) = @_; Eat Exporter’s Brain

Slide 313

Slide 313 text

sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; Eat Exporter’s Brain

Slide 314

Slide 314 text

sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; Sub::Exporter::setup_exporter({ Eat Exporter’s Brain

Slide 315

Slide 315 text

sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; Sub::Exporter::setup_exporter({ as => ‘import’, Eat Exporter’s Brain

Slide 316

Slide 316 text

sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; Sub::Exporter::setup_exporter({ as => ‘import’, into => $new_pkg, Eat Exporter’s Brain

Slide 317

Slide 317 text

sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; Sub::Exporter::setup_exporter({ as => ‘import’, into => $new_pkg, exports => [ @{“$pkg\::EXPORT_OK”} ], Eat Exporter’s Brain

Slide 318

Slide 318 text

sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; Sub::Exporter::setup_exporter({ as => ‘import’, into => $new_pkg, exports => [ @{“$pkg\::EXPORT_OK”} ], groups => { Eat Exporter’s Brain

Slide 319

Slide 319 text

sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; Sub::Exporter::setup_exporter({ as => ‘import’, into => $new_pkg, exports => [ @{“$pkg\::EXPORT_OK”} ], groups => { %{“$pkg\::EXPORT_TAGS”}, Eat Exporter’s Brain

Slide 320

Slide 320 text

sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; Sub::Exporter::setup_exporter({ as => ‘import’, into => $new_pkg, exports => [ @{“$pkg\::EXPORT_OK”} ], groups => { %{“$pkg\::EXPORT_TAGS”}, default => [ @{“$pkg\::EXPORTS”} ], Eat Exporter’s Brain

Slide 321

Slide 321 text

sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; Sub::Exporter::setup_exporter({ as => ‘import’, into => $new_pkg, exports => [ @{“$pkg\::EXPORT_OK”} ], groups => { %{“$pkg\::EXPORT_TAGS”}, default => [ @{“$pkg\::EXPORTS”} ], }, Eat Exporter’s Brain

Slide 322

Slide 322 text

sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; Sub::Exporter::setup_exporter({ as => ‘import’, into => $new_pkg, exports => [ @{“$pkg\::EXPORT_OK”} ], groups => { %{“$pkg\::EXPORT_TAGS”}, default => [ @{“$pkg\::EXPORTS”} ], }, }); Eat Exporter’s Brain

Slide 323

Slide 323 text

sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; Sub::Exporter::setup_exporter({ as => ‘import’, into => $new_pkg, exports => [ @{“$pkg\::EXPORT_OK”} ], groups => { %{“$pkg\::EXPORT_TAGS”}, default => [ @{“$pkg\::EXPORTS”} ], }, }); push @{“$new_pkg\::ISA”}, $class; Eat Exporter’s Brain

Slide 324

Slide 324 text

sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; Sub::Exporter::setup_exporter({ as => ‘import’, into => $new_pkg, exports => [ @{“$pkg\::EXPORT_OK”} ], groups => { %{“$pkg\::EXPORT_TAGS”}, default => [ @{“$pkg\::EXPORTS”} ], }, }); push @{“$new_pkg\::ISA”}, $class; return $new_pkg; Eat Exporter’s Brain

Slide 325

Slide 325 text

sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; Sub::Exporter::setup_exporter({ as => ‘import’, into => $new_pkg, exports => [ @{“$pkg\::EXPORT_OK”} ], groups => { %{“$pkg\::EXPORT_TAGS”}, default => [ @{“$pkg\::EXPORTS”} ], }, }); push @{“$new_pkg\::ISA”}, $class; return $new_pkg; } Eat Exporter’s Brain

Slide 326

Slide 326 text

No content

Slide 327

Slide 327 text

package UNIVERSAL;

Slide 328

Slide 328 text

package UNIVERSAL; sub exporter_upgrade {

Slide 329

Slide 329 text

package UNIVERSAL; sub exporter_upgrade { my ($pkg) = @_;

Slide 330

Slide 330 text

package UNIVERSAL; sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”;

Slide 331

Slide 331 text

package UNIVERSAL; sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; return $new_pkg if $new_pkg->isa($pkg);

Slide 332

Slide 332 text

package UNIVERSAL; sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; return $new_pkg if $new_pkg->isa($pkg); Sub::Exporter::setup_exporter({

Slide 333

Slide 333 text

package UNIVERSAL; sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; return $new_pkg if $new_pkg->isa($pkg); Sub::Exporter::setup_exporter({ as => ‘import’,

Slide 334

Slide 334 text

package UNIVERSAL; sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; return $new_pkg if $new_pkg->isa($pkg); Sub::Exporter::setup_exporter({ as => ‘import’, into => $new_pkg,

Slide 335

Slide 335 text

package UNIVERSAL; sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; return $new_pkg if $new_pkg->isa($pkg); Sub::Exporter::setup_exporter({ as => ‘import’, into => $new_pkg, exports => [ @{“$pkg\::EXPORT_OK”} ],

Slide 336

Slide 336 text

package UNIVERSAL; sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; return $new_pkg if $new_pkg->isa($pkg); Sub::Exporter::setup_exporter({ as => ‘import’, into => $new_pkg, exports => [ @{“$pkg\::EXPORT_OK”} ], groups => {

Slide 337

Slide 337 text

package UNIVERSAL; sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; return $new_pkg if $new_pkg->isa($pkg); Sub::Exporter::setup_exporter({ as => ‘import’, into => $new_pkg, exports => [ @{“$pkg\::EXPORT_OK”} ], groups => { %{“$pkg\::EXPORT_TAGS”},

Slide 338

Slide 338 text

package UNIVERSAL; sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; return $new_pkg if $new_pkg->isa($pkg); Sub::Exporter::setup_exporter({ as => ‘import’, into => $new_pkg, exports => [ @{“$pkg\::EXPORT_OK”} ], groups => { %{“$pkg\::EXPORT_TAGS”}, default => [ @{“$pkg\::EXPORTS”} ],

Slide 339

Slide 339 text

package UNIVERSAL; sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; return $new_pkg if $new_pkg->isa($pkg); Sub::Exporter::setup_exporter({ as => ‘import’, into => $new_pkg, exports => [ @{“$pkg\::EXPORT_OK”} ], groups => { %{“$pkg\::EXPORT_TAGS”}, default => [ @{“$pkg\::EXPORTS”} ], },

Slide 340

Slide 340 text

package UNIVERSAL; sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; return $new_pkg if $new_pkg->isa($pkg); Sub::Exporter::setup_exporter({ as => ‘import’, into => $new_pkg, exports => [ @{“$pkg\::EXPORT_OK”} ], groups => { %{“$pkg\::EXPORT_TAGS”}, default => [ @{“$pkg\::EXPORTS”} ], }, });

Slide 341

Slide 341 text

package UNIVERSAL; sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; return $new_pkg if $new_pkg->isa($pkg); Sub::Exporter::setup_exporter({ as => ‘import’, into => $new_pkg, exports => [ @{“$pkg\::EXPORT_OK”} ], groups => { %{“$pkg\::EXPORT_TAGS”}, default => [ @{“$pkg\::EXPORTS”} ], }, }); push @{“$new_pkg\::ISA”}, $class;

Slide 342

Slide 342 text

package UNIVERSAL; sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; return $new_pkg if $new_pkg->isa($pkg); Sub::Exporter::setup_exporter({ as => ‘import’, into => $new_pkg, exports => [ @{“$pkg\::EXPORT_OK”} ], groups => { %{“$pkg\::EXPORT_TAGS”}, default => [ @{“$pkg\::EXPORTS”} ], }, }); push @{“$new_pkg\::ISA”}, $class; return $new_pkg;

Slide 343

Slide 343 text

package UNIVERSAL; sub exporter_upgrade { my ($pkg) = @_; my $new_pkg = “$pkg\::SE”; return $new_pkg if $new_pkg->isa($pkg); Sub::Exporter::setup_exporter({ as => ‘import’, into => $new_pkg, exports => [ @{“$pkg\::EXPORT_OK”} ], groups => { %{“$pkg\::EXPORT_TAGS”}, default => [ @{“$pkg\::EXPORTS”} ], }, }); push @{“$new_pkg\::ISA”}, $class; return $new_pkg; }

Slide 344

Slide 344 text

Fixing caller

Slide 345

Slide 345 text

Fixing caller sub default_exporter {

Slide 346

Slide 346 text

Fixing caller sub default_exporter { my ($class, $gen, $name, $arg, $col, $as, $into)

Slide 347

Slide 347 text

Fixing caller sub default_exporter { my ($class, $gen, $name, $arg, $col, $as, $into) = @_;

Slide 348

Slide 348 text

Fixing caller sub default_exporter { my ($class, $gen, $name, $arg, $col, $as, $into) = @_; _install(

Slide 349

Slide 349 text

Fixing caller sub default_exporter { my ($class, $gen, $name, $arg, $col, $as, $into) = @_; _install( _generate($class, $generator, $name, $arg, $col),

Slide 350

Slide 350 text

Fixing caller sub default_exporter { my ($class, $gen, $name, $arg, $col, $as, $into) = @_; _install( _generate($class, $generator, $name, $arg, $col), $into,

Slide 351

Slide 351 text

Fixing caller sub default_exporter { my ($class, $gen, $name, $arg, $col, $as, $into) = @_; _install( _generate($class, $generator, $name, $arg, $col), $into, $as,

Slide 352

Slide 352 text

Fixing caller sub default_exporter { my ($class, $gen, $name, $arg, $col, $as, $into) = @_; _install( _generate($class, $generator, $name, $arg, $col), $into, $as, );

Slide 353

Slide 353 text

Fixing caller sub default_exporter { my ($class, $gen, $name, $arg, $col, $as, $into) = @_; _install( _generate($class, $generator, $name, $arg, $col), $into, $as, ); }

Slide 354

Slide 354 text

No content

Slide 355

Slide 355 text

sub evil_eval_exporter { # TOTALLY UNTESTED!

Slide 356

Slide 356 text

sub evil_eval_exporter { # TOTALLY UNTESTED! my ($class, $gen, $name, $arg, $col, $as, $into)

Slide 357

Slide 357 text

sub evil_eval_exporter { # TOTALLY UNTESTED! my ($class, $gen, $name, $arg, $col, $as, $into) = @_;

Slide 358

Slide 358 text

sub evil_eval_exporter { # TOTALLY UNTESTED! my ($class, $gen, $name, $arg, $col, $as, $into) = @_;

Slide 359

Slide 359 text

sub evil_eval_exporter { # TOTALLY UNTESTED! my ($class, $gen, $name, $arg, $col, $as, $into) = @_; $col->{_g} ||= do {

Slide 360

Slide 360 text

sub evil_eval_exporter { # TOTALLY UNTESTED! my ($class, $gen, $name, $arg, $col, $as, $into) = @_; $col->{_g} ||= do { my $g = Dump(\&_generate)->Names(‘GEN’)->Out;

Slide 361

Slide 361 text

sub evil_eval_exporter { # TOTALLY UNTESTED! my ($class, $gen, $name, $arg, $col, $as, $into) = @_; $col->{_g} ||= do { my $g = Dump(\&_generate)->Names(‘GEN’)->Out; $g =~ s/\A\$GEN = sub/sub _generate/;

Slide 362

Slide 362 text

sub evil_eval_exporter { # TOTALLY UNTESTED! my ($class, $gen, $name, $arg, $col, $as, $into) = @_; $col->{_g} ||= do { my $g = Dump(\&_generate)->Names(‘GEN’)->Out; $g =~ s/\A\$GEN = sub/sub _generate/; $g = “package $into;\n$g”;

Slide 363

Slide 363 text

sub evil_eval_exporter { # TOTALLY UNTESTED! my ($class, $gen, $name, $arg, $col, $as, $into) = @_; $col->{_g} ||= do { my $g = Dump(\&_generate)->Names(‘GEN’)->Out; $g =~ s/\A\$GEN = sub/sub _generate/; $g = “package $into;\n$g”; eval $g;

Slide 364

Slide 364 text

sub evil_eval_exporter { # TOTALLY UNTESTED! my ($class, $gen, $name, $arg, $col, $as, $into) = @_; $col->{_g} ||= do { my $g = Dump(\&_generate)->Names(‘GEN’)->Out; $g =~ s/\A\$GEN = sub/sub _generate/; $g = “package $into;\n$g”; eval $g; \&{“$into\::_generate”};

Slide 365

Slide 365 text

sub evil_eval_exporter { # TOTALLY UNTESTED! my ($class, $gen, $name, $arg, $col, $as, $into) = @_; $col->{_g} ||= do { my $g = Dump(\&_generate)->Names(‘GEN’)->Out; $g =~ s/\A\$GEN = sub/sub _generate/; $g = “package $into;\n$g”; eval $g; \&{“$into\::_generate”}; };

Slide 366

Slide 366 text

sub evil_eval_exporter { # TOTALLY UNTESTED! my ($class, $gen, $name, $arg, $col, $as, $into) = @_; $col->{_g} ||= do { my $g = Dump(\&_generate)->Names(‘GEN’)->Out; $g =~ s/\A\$GEN = sub/sub _generate/; $g = “package $into;\n$g”; eval $g; \&{“$into\::_generate”}; }; _install(

Slide 367

Slide 367 text

sub evil_eval_exporter { # TOTALLY UNTESTED! my ($class, $gen, $name, $arg, $col, $as, $into) = @_; $col->{_g} ||= do { my $g = Dump(\&_generate)->Names(‘GEN’)->Out; $g =~ s/\A\$GEN = sub/sub _generate/; $g = “package $into;\n$g”; eval $g; \&{“$into\::_generate”}; }; _install( $col->{_g}($class, $generator, $name, $arg, $col),

Slide 368

Slide 368 text

sub evil_eval_exporter { # TOTALLY UNTESTED! my ($class, $gen, $name, $arg, $col, $as, $into) = @_; $col->{_g} ||= do { my $g = Dump(\&_generate)->Names(‘GEN’)->Out; $g =~ s/\A\$GEN = sub/sub _generate/; $g = “package $into;\n$g”; eval $g; \&{“$into\::_generate”}; }; _install( $col->{_g}($class, $generator, $name, $arg, $col), $into,

Slide 369

Slide 369 text

sub evil_eval_exporter { # TOTALLY UNTESTED! my ($class, $gen, $name, $arg, $col, $as, $into) = @_; $col->{_g} ||= do { my $g = Dump(\&_generate)->Names(‘GEN’)->Out; $g =~ s/\A\$GEN = sub/sub _generate/; $g = “package $into;\n$g”; eval $g; \&{“$into\::_generate”}; }; _install( $col->{_g}($class, $generator, $name, $arg, $col), $into, $as,

Slide 370

Slide 370 text

sub evil_eval_exporter { # TOTALLY UNTESTED! my ($class, $gen, $name, $arg, $col, $as, $into) = @_; $col->{_g} ||= do { my $g = Dump(\&_generate)->Names(‘GEN’)->Out; $g =~ s/\A\$GEN = sub/sub _generate/; $g = “package $into;\n$g”; eval $g; \&{“$into\::_generate”}; }; _install( $col->{_g}($class, $generator, $name, $arg, $col), $into, $as, );

Slide 371

Slide 371 text

sub evil_eval_exporter { # TOTALLY UNTESTED! my ($class, $gen, $name, $arg, $col, $as, $into) = @_; $col->{_g} ||= do { my $g = Dump(\&_generate)->Names(‘GEN’)->Out; $g =~ s/\A\$GEN = sub/sub _generate/; $g = “package $into;\n$g”; eval $g; \&{“$into\::_generate”}; }; _install( $col->{_g}($class, $generator, $name, $arg, $col), $into, $as, ); }

Slide 372

Slide 372 text

Thank You!