Deploying Perl 6 - Pugspugs.blogs.com/talks/boston-deploying-perl6.pdf · Pugs 6.2.12 •Released...

Preview:

Citation preview

DeployingPerl 6

AudreyTang

1

Perl6ishereToday!

2

Perl6ishereToday!(YAPC::NA2005)

3

Pugs6.2.12•ReleasedonJune26th

•3xfasterbuildtime

•10xfastercompilation

•2xfasterruntime

•2000+commitssince6.2.11

4

Parrot0.4.5•ReleasedlastJune19th

•Unicodeidentifiers

•Hierarchicalnamespace

•New.NETCLRtranslator

•Muchfastercompilertools

5

Greatforexperimenting

6

Butnotforproduction

7

...notthisChristmas

8

9

CPANisthelanguage

10

Perlisjustitssyntax

11

Perl5.000b3h(October1994)

12

• use5.000;• usestrict;• require'fastcwd.pl';• require'newgetopt.pl';• require'exceptions.pl';• #...•

13

Continuity++

14

Pugs6.2.2

(June2005)

15

• use v6-pugs;• use perl5:DBI;• use perl5:Encode;• use perl5:Template;• # ...•

16

StillneedtoinstallPugs

17

Perl5.9.3

(Jan2006)

18

• usev5.9.3;• usefeatureqw(switchsayerr~~);

• given(shift()){• when['‐h','‐‐help']{• say"Usage:$0";• }• default{• $0~~'moose.exe'errdie"NotMoose";• }• }•

19

HowtogetPerl6intoProduction?

20

Production

• Workwithexistingcode

• MustsupportPerl5andXS

• Nofrom‐scratchrewrites

21

Frontends?

Parrot

Tcl Python Scheme

22

Frontends?

Parrot

Tcl Python Scheme

Perl 5(Ponie)

Perl 6

23

Backends!

Pugs

HaskellJava

ScriptPerl 5

24

Backends!

Pugs

JavaScript

Haskell Perl 5

YARV?JVM?

CLR? PyPy?

25

PugsonPerl5

26

Perl6RuntimeImplementedasPerl5Modules

27

SanePerl5(notsourcefilters)

28

AvailableOnCPANToday

29

Moose.pm☯

30

WhatisMoose?

• CompleteobjectmodelforPerl5

• BasedonthePerl6objectmodel

• ProductionReady

31

Moose,it’sthenewCamel

32

ObjectsWithClass

33

• usev6‐pugs;• classPoint;• • • has$.xisrw;#instanceattributes• has$.y;#default"isreadonly"• • methodclear(){

• $.x=0;#accessor• $!y=0;#directslotaccess• }•

34

• usev5;• packagePoint;• useMoose;• • hasx=>(is=>'rw');• hasy=>(is=>'ro');• • subclear{• my$self=shift;• $self‐>x(0);#accessor• $self‐>{y}=0;#directslotaccess• }•

35

Subclassing

36

• usev6‐pugs;• classPoint3D;• • • isPoint;• • has$.z;• • methodclear(){• • call;• $!z=0;• };•

37

• usev5;• packagePoint3D;• useMoose;• • extends'Point';• • hasz=>(isa=>'Int');• • overrideclear=>sub{• my$self=shift;• super;• $self‐>{z}=0;• };•

38

• usev5;• packagePoint3D;• useMoose;• • extends'Point';• • hasz=>(isa=>'Int');• • afterclear=>sub{• my$self=shift;• • $self‐>{z}=0;• };•

39

MetaObjects

40

• usev6‐pugs;

• Foo.meta.add_method(bar=>method{• say"HellofromFoo.bar";• });• Foo.meta.superclasses;• Foo.meta.compute_all_applicable_methods;• Foo.meta.find_all_methods_by_name($method_name);

41

• usev5;• useMoose;• Foo‐>meta‐>add_method(bar=>sub{• print"HellofromFoo‐>bar\n";• });• Foo‐>meta‐>superclasses;• Foo‐>meta‐>compute_all_applicable_methods;• Foo‐>meta‐>find_all_methods_by_name($method_name);

42

Laziness

43

• usev6‐pugs;• classBinaryTreeisrw;• • hasAny$.node;• hasBinaryTree$.parenthandles{• parent_node=>'node'• };• hasBinaryTree$.left={• lazy{BinaryTree.new(parent=>self)}• };• hasBinaryTree$.right={• lazy{BinaryTree.new(parent=>self)}• };•

44

• usev5;• packageBinaryTree;• useMoose;• • hasnode=>(is=>'rw',isa=>'Any');• hasparent=>(• is=>'rw',• isa=>'BinaryTree',• handles=>{parent_node=>'node'},• weak_ref=>1,• );• hasleft=>(• is=>'rw',• isa=>'BinaryTree',• default=>sub{BinaryTree‐>new(parent=>$_[0])},• lazy=>1,• );

• #dittofor“hasright”

45

TypeConstraints

46

• usev6‐pugs;• classAddress;• useperl5:Locale::US;• useperl5:Regexp::Common<zip$RE>;• • my$STATES=Locale::US.new;• subsetUS_StateofStrwhere{• $STATES{any(<code2statestate2code>)}{.uc};• };• • hasStr$.streetisrw;• hasStr$.cityisrw;• hasUS_State$.stateisrw;• hasStr$.zip_codeisrwwhere{• $_~~$RE<zip><US>{'‐extended'=>'allow'}• };•

47

• usev5;• packageAddress;• useMoose;• useMoose::Util::TypeConstraints;• useLocale::US;• useRegexp::Common'zip';• • my$STATES=Locale::US‐>new;• subtypeUSState=>asStr=>where{• $STATES‐>{code2state}{uc($_)}• or$STATES‐>{state2code}{uc($_)};• }• • hasstreet=>(is=>'rw',isa=>'Str');• hascity=>(is=>'rw',isa=>'Str');• hasstate=>(is=>'rw',isa=>'USState');• haszip_code=>(• is=>'rw',• isa=>subtypeStr=>where{• /$RE{zip}{US}{‐extended=>'allow'}/• },• );•

48

Roles

49

• usev6‐pugs;• roleEquality;• • methodequal_to($other){...}• • methodnot_equal_to($other){

• not$self‐>equal_to($other);• }• •

50

• usev5;• packageEquality;• useMoose::Role;• requires'equal_to';• • subnot_equal_to{• my($self,$other)=@_;• not$self‐>equal_to($other);• }• •

51

DerivedRoles

52

• usev6‐pugs;• roleComparable;• doesEquality;• • methodcompare($other){...}

• methodequal_to($other){

• $self‐>compare($other)==0;• }• • methodgreater_than($other){

• $self‐>compare($other)==1;• }• • methodless_than($other){

• $self‐>compare($other)==‐1;• }•

53

• usev5;• packageComparable;• with'Equality';• • requires'compare';• • subequal_to{• my($self,$other)=@_;• $self‐>compare($other)==0;• }• • subgreater_than{• my($self,$other)=@_;• $self‐>compare($other)==1;• }• • subless_than{• my($self,$other)=@_;• $self‐>compare($other)==‐1;• }•

54

AbstractInterfaceRoles

55

• usev6‐pugs;• rolePrintable;

• • methodto_string{...}

56

• usev5;• packagePrintable;• useMoose::Role;• • requires'to_string';

57

MixingInRoles

58

• usev6‐pugs;• classPerson;• doesComparable;• doesPrintable;• • hasStr$.first_name;• hasStr$.last_name;• • methodto_string(){• • "$.last_name,$.first_name";• }• • methodcompare($other){• • $.to_stringcmp$other.to_string;• }•

59

• usev5;• packagePerson;• useMoose;• with'Comparable','Printable';• • hasfirst_name=>(isa=>'Str');• haslast_name=>(isa=>'Str');• • subto_string{• my$self=shift;• $self‐>last_name.','.$self‐>first_name;• }• • subcompare{• my($self,$other)=@_;• $self‐>to_stringcmp$other‐>to_string;• }•

60

•AutomaticCoercion

•AccessorTriggers

•Alternatelayouts!

Morefeatures

61

Pugs::Compiler::Rule☯

62

RegexObjects

63

• usev6‐pugs;

• my$txt='Car‐ModelT,1909';• my$pat=rx{• Car‐• [(Ferrari)• |(ModelT,(\d\d\d\d))• ]• };• $txt~~$paterrfail"Cannotmatch";•

64

• usev5;• usePugs::Compiler::Regex;• my$txt='Car‐ModelT,1909';• my$pat=Pugs::Compiler::Regex‐>compile(q(• Car‐• [(Ferrari)• |(ModelT,(\d\d\d\d))• ]• ));• $pat‐>match($txt)ordie"Cannotmatch";•

65

MatchObjects

66

• usev6‐pugs;

• my$pat=rx{• Car‐[• (Ferrari)|(ModelT,(\d\d\d\d))• ]• };

• my$match=('Car‐ModelT,1909'~~$pat);• say$match;#"Car‐ModelT,1909"• say$match[0];#undef• say$match[1];#"ModelT,1909"• say$match[1][0];#"1909"• say$match[1][0].from;#11• say$match[1][0].to;#15•

67

• usev5;• usePugs::Compiler::Regex;• my$pat=Pugs::Compiler::Regex‐>compile(q(• Car‐[• (Ferrari)|(ModelT,(\d\d\d\d))• ]• ));

• usefeatureqw(say);• my$match=$pat‐>match('Car‐ModelT,1909');• say$match;#"Car‐ModelT,1909"• say$match‐>[0];#undef• say$match‐>[1];#"ModelT,1909"• say$match‐>[1][0];#"1909"• say$match‐>[1][0]‐>from;#11• say$match‐>[1][0]‐>to;#15•

68

NamedCaptures

69

• usev6‐pugs;• • my$pat=rx{• Car‐[• (Ferrari)• |(ModelT,$<year>:=[\d\d\d\d])• ]• };

• my$match=('Car‐ModelT,1909'~~$pat);• say$match;#"Car‐ModelT,1909"• say$match[1];#"ModelT,1909"• say$match[1]<year>;#"1909"• say$match[1]<year>.from;#11• say$match[1]<year>.to;#15•

70

• usev5;• usePugs::Compiler::Regex;• my$pat=Pugs::Compiler::Regex‐>compile(q(• Car‐[• (Ferrari)• |(ModelT,$<year>:=[\d\d\d\d])• ]• ));

• usefeatureqw(say);• my$match=$pat‐>match('Car‐ModelT,1909');• say$match;#"Car‐ModelT,1909"• say$match‐>[1];#"ModelT,1909"• say$match‐>[1]{year};#"1909"• say$match‐>[1]{year}‐>from;#11• say$match‐>[1]{year}‐>to;#15•

71

GrammarModules

72

• usev6‐pugs;• • grammarCarInfo;• • regexcar{• Car‐[(Ferrari)|(ModelT,<year>)]• }• regexyear{• \d\d\d\d• }• • moduleMain;• my$match=('Car‐ModelT,1909'~~CarInfo.car);•

73

• usev5;• usePugs::Compiler::Regex;• packageCarInfo;• usebase'Pugs::Grammar::Base';• *Car‐Pugs::Compiler::Regex‐>compile(q(• Car‐[(Ferrari)|(ModelT,<year>)]• ))‐>code;• *year=Pugs::Compiler::Regex‐>compile(q(• \d\d\d\d• ))‐>code;• • packagemain;• my$match=CarInfo‐>car('Car‐ModelT,1909');•

74

ResultObjects

75

• #XXX‐TypicalPerl5code• usev5;• my$txt='Car‐ModelT,1909';• my$pat=qr{• Car‐(?:(Ferrari)|(ModelT,(\d\d\d\d)))• }x;

• my$obj;• if($txt=~$pat){• if($1){• $obj=Car‐>new(color=>"red");• }elsif($2){• $obj=Car‐>new(color=>"black",year=>$3);• }• }•

76

• usev6‐pugs;

• my$txt='Car‐ModelT,1909';• my$pat=rx{• Car‐[Ferrari• {returnCar.new(:color<red>)}• |ModelT,$<year>:=[\d\d\d\d]• {returnCar.new(:color<black>:$<year>)}• ]• };

• my$obj=$($txt~~$pat);• print$obj<year>;#1909

77

• usev5;• usePugs::Compiler::Regex;• my$txt='Car‐ModelT,1909';• my$pat=Pugs::Compiler::Regex‐>compile(q(• Car‐[Ferrari• {returnCar‐>new(color=>'red')}• |ModelT,$<year>:=[\d\d\d\d]• {returnCar‐>new(• color=>'black',year=>$<year>)}• ]• ));• my$obj=$pat‐>match($txt)‐>();• print$obj‐>{year};#1909

78

BacktrackControl

79

• usev6‐pugs;• "ModelT2005"~~regex{• Car‐ModelT\d*;• };

• usev5;• "ModelT2005"=~qr{• Car‐ModelT\d*;• }x;

80

• usev6‐pugs;• "ModelT2005"~~token{• Car‐ModelT\d*;• };

• usev5;• "ModelT2005"=~qr{• Car‐ModelT(?>\d*);• }x;

81

• usev6‐pugs;• "ModelT2005"~~rule{• Car‐ModelT\d*;• };

• usev5;• "ModelT2005"=~qr{• Car\s*=\s*ModelT\s+(?>\d*)\s*;• }x;

82

MoreComponents

•Pugs::Grammar::Precedence

•Pugs::Grammar::MiniPerl6

•Pugs::Compiler::RegexPerl5

83

Module::Compile☯

84

EveryonehatesSpiffy

85

• usev5;• useSpiffy‐Base;

• mysubprivate{• "It'saprivatemethodhere";• }

• subpublic{• $self‐>$private;• }

• subnew(){• my$self=super;• $self‐>init;• return$self;• }•

86

ToomuchMagic

87

YAMLusedSpiffy

88

Test::BaseusesSpiffy

89

IO::AllusesSpiffy

90

KwikiusesIO::All

91

Ergo...

92

EveryonehatesIngy

93

What'shatefulaboutSpiffy?

94

It'saSourceFilter!

95

• usev5;• useFilter::Simplesub{• s{(^sub\s+\w+\s+\{)}• {$1\nmy$self=shift;\n}mgx;• }•

96

•Addsdependency

•Slowsdownstartup

•Breaksperl‐d

•WrecksotherSourceFilters

Filter::SimpleBad

97

Wecanfixit!

98

• usev5;• useFilter::Simplesub{• s{(^sub\s+\w+\s+\{)}• {$1\nmy$self=shift;\n}mgx;• }

99

• usev5;• useFilter::Simple::Compilesub{• s{(^sub\s+\w+\s+\{)}• {$1\nmy$self=shift;\n}mgx;• }

100

Howdoesthatwork?

101

Little‐knownfact:

102

“useFoo”looksforFoo.pmcbeforeFoo.pm

103

• %echo'print"Hello\n"'>Foo.pmc• %perl‐MFoo‐e1• Hello

104

Saveprecompiledresultto.pmc...

105

...nofilteringneedednexttime!

106

•Freeofuser‐sidedependencies

•Faststartuptime

•Debuggablesourceisallin.pmc

•Allowscomposableprecompilers

Module::CompileGood

107

• packageFoo;• useModule::Compile‐base;• • subpmc_compile{• my($class,$source,$context)=@_;• #Convert$sourceinto$compiled_output...• return$compiled_output;• }•

108

Filter::Simple::Compile

109

• #Drop‐inreplacementtoFilter::Simple• packageAcme::Y2K;• useFilter::Simple::Compilesub{• tr/y/k/;• }•

110

• #It'slexical!• my$normal_code_here;• {• useAcme::Y2K;• pacyageFoo;• mydir"tmp";• }• my$normal_code_there;•

111

Filter::Macro

112

• packageMyHandyModules;• useFilter::Macro;

• #linesbelowwillbeexpandedintocaller'scode• usestrict;• usewarnings;• useFatalqw(openclose);• useFindBinqw($Bin);•

113

• #Inyourcode• packageMyApp;• useMyHandyModules;• print"I'minvokedfrom$Bin";•

114

• #Inyourcode• packageMyApp;

• usestrict;• usewarnings;• useFatalqw(openclose);• useFindBinqw($Bin);

• #line3• print"I'minvokedfrom$Bin";•

115

• #Makefile.PL• useinc::Module::Install;• • name'MyApp';• all_from'lib/MyApp.pm';• • pmc_support;

• WriteAll;•

116

Nodependencyon

MyHandyModules.pm

117

Inline::Module

118

• #Aww...• packageMyApp;• useFile::Slurpqw(slurp);• useHTTP::MessageParser;•

119

• #Yay!• packageMyApp;• useInline::Module'File::Slurp'=>qw(slurp);• useInline::Module'HTTP::MessageParser';•

120

ZeroDependencies

121

WhataboutDeployingPerl6?

122

usev6‐alpha;

123

v6.pm(onCPANnow!)

124

WritePerl6compiletoPerl5

125

Source:Rule.pm

126

• usev6‐pugs;• • grammarPugs::Grammar::Rule;• rulews:P5{• ^((?:\s|\#(?‐s:.)*)+)• }• #...morerules...

127

Target:Rule.pmc

128

• #Generatedbyv60(Module::Compile0.15)‐donotedit!• #line1##########(((32‐bitChecksumValidator)))##################• BEGIN{use5.006;local(*F,$/);($F=__FILE__)=~s!c$!!;open(F)• ordie"Cannotopen$F:$!";binmode(F,':crlf');unpack('%32N*',<F>)• ==0x1D6399E1ordie"Checksumfailedforoutdated.pmcfile:${F}c"}• #line1#############################################################• packagePugs::Grammar::Rule;• usebase'Pugs::Grammar::Base';• *{'Pugs::Grammar::Rule::ws'}=sub{• my$grammar=shift;• #warn"ruleargumentisundefined"unlessdefined$_[0];• $_[0]=""unlessdefined$_[0];• my$bool=$_[0]=~/^((?:\s|\#(?‐s:.)*)+)(.*)$/sx;• return{• bool=>$bool,• match=>$1,• tail=>$2,• #capture=>$1,• }• };• #...morerules...

129

“CPANisthelanguage,Perl6isjustabetter

syntaxsugar!”

130

Stillneedswork!

131

In Progress➳

132

Augmentation (XS)PadWalker, Devel::Caller

autobox, re::override ...

SemanticsData::Bind, Class::MOP

Pugs::Runtime, Pugs::Compiler::Rule ...

Perl 5 SugarMoose, Moose::Autobox ...

Syntaxv6.pm

Pugs::Compiler::Perl6 ...

Core

perl

Tool SupportCPAN, PAUSE

Perldoc, Perl::Tidy ...

!"#$%&

'()&*+,&-"#%

.&*%!/01!2.34!1"#56

InfrastructureParse::Yapp

Module::Compile ...

133

NativeTypeBridgeMoose::Autobox

134

BuiltinObjectsPugs::Runtime::*

135

CallingConventionData::Bind

136

MultiDispatchSub::Multi

137

EvenMoreSugarre::override

138

TranslatorsMAD➠Perl6

139

Multiversioningonly.pm

140

• #Theusecase• usev5;• packageFoo1;subnew{bless{},'Foo1'}• packageFoo2;subnew{bless{},'Foo2'}• packageTest1;subt1{Common‐>foo}• packageTest2;subt2{Common‐>foo}• packageCommon;subfoo{Foo‐>new}

• packagemain;• warnTest1::t1();#Foo1object• warnTest2::t2();#Foo2object

•Foo

Common

Test1 Test2

Foo1 Foo2

141

• #Theimplementation• usev5;• BEGIN{$^P|=0x01}• • my%dep_map=(• Test1=>{Foo=>'Foo1'},• Test2=>{Foo=>'Foo2'},• );• • subDB::sub{• my$cls=(split(/::/,$DB::sub,2))[0];• while(my($k,$v)=each%{$dep_map{$cls}}){• %{"$k\::"}=%{"$v\::"};• }• goto&$DB::sub;• }•

142

CommitsWelcome!

143

WhenwillPerl6bereleased?

144

ByChristmas!

145

WhenPerl6isout,everydaywillbelike

Christmas!❆

146

TheAdventstartsNow

147

148

Thank you!☺

149

Recommended