A talk about XS Fun, an easy-going tutorial to Perl XS:
https://github.com/xsawyerx/xs-fun
This talk was originally given at Cluj.pm, on November 7th, 2013.
2. XS is...
A glue layer between Perl and C/C++
Sections, macros, functions
Requires knowledge with C, Perl API, XS, typemaps
Written by our very best
Complex, difficult, strenuous, laborious, painful, formidable
(additional words can be found in thesaurus.com )
An impediment to Modern Perl
4. XS is
misunderstood
No need to be an expert
Basic Perl API is simple
XS is not too difficult
Lots of options
All documented
5. Except...
... except those that aren't documented
... except that the official tutorial sucks
... except that there's no better (or any other full) tutorial
(at least not one I could find)
7. XS Fun
A tutorial making XS fun
Starts easy, progresses slowly
Split to chapters, each taking a small task
A lot of explanations for beginners
All examples work
10. Quick glossary
SV = Scalar Value
Of:
IV = Integer Value (m $ = - 0
y i
3)
UV = Unsigned Integer Value (m $ = 3 0 . .
y u
00.)
NV = Double Value (m $ = 3 . 7 )
y d
071
PV = String Value (m $ = ' e l w r d )
y s
hlo ol'
SV = (another) Scalar Value
AH = Array Value (m @ = q < e l w r d )
y a
whlo ol>
HV = Hash Value (m % = ( h l o = ' o l ' )
y h
el > wrd )
RV = Reference Value (m $ a h e = h
y hsrf
%)
That's it for now
12. XSFun.pm
pcaeXFn
akg Su;
uesrc;
s tit
uewrig;
s anns
ueEpre;
s xotr
ueXLae;
s Sodr
uebs 'xotr;
s ae Epre'
or$ESO
u VRIN
='.0'
001;
or%XOTTG =('l'= [wgtvrin])
u EPR_AS
al > q<e_eso> ;
or@XOTO
u EPR_K =(@ $XOTTG{al}})
{ EPR_AS'l'
;
XLae:la('Su' $ESO )
Sodr:od XFn, VRIN ;
1
;
13. XSFun.xs
#nld "XENh
icue ETR."
#nld "elh
icue pr."
#nld "SBh
icue XU."
#nld "poth
icue ppr."
MDL =XFn
OUE
Su
PCAE=XFn
AKG
Su
dul
obe
adnmesdul a dul b
d_ubr(obe , obe )
CD:
OE
RTA =a+b
EVL
;
OTU:
UPT
RTA
EVL
S *
V
adnmespr(V*,S *)
d_ubr_elS a V b
CD:
OE
{
cntdul sm=SN()+SN()
os obe u
vVa
vVb;
RTA =nwVvsm;
EVL
eSn(u)
}
OTU:RTA
UPT EVL
14. XSFun.xs (more)
#nld <hoarn.>
icue crmpith
/ i tecd scin*
* n h oe eto /
cntca *
os hr
gtvrin)
e_eso(
CD:
OE
RTA =crmpitgtvrin)
EVL
hoarn_e_eso(;
OTU:RTA
UPT EVL
#i vrint
n eso.:
ueXFn'al;
s Su :l'
i(gtvrin) '..' 'hoarn vrini 600 )
s e_eso(, 600, crmpit eso s ..' ;
tvrint. o
/eso. . k
Altssscesu.
l et ucsfl
Fls1 Tss1 0wlcoksc (00 ur 00 ss+ 00 cs
ie=, et=,
allc es
.3 s
.1 y
.2 ur
00 cy = 00 CU
.0 ss
.6 P)
Rsl:PS
eut AS
19. Constructor
#I Pr:
n el
sbnw{bes{,sit}
u e
ls } hf
#o..
r.
sbnw{
u e
m $ls =sit #o $[]
y cas
hf;
r _0
m %ah =(;
y hs
)
m $ef =hs;
y sl
%ah
rtr bes$ef $ls;
eun ls sl, cas
}
20. #Pr-el
uePr
sbnw{
u e
m $ls =sit #o $[]
y cas
hf;
r _0
m %ah =(;
y hs
)
m $ef =hs;
y sl
%ah
rtr bes$ef $ls;
eun ls sl, cas
}
/ i X *
* n S /
S *
V
nw cntca *ls )
e( os hr cas
CD:
OE
/ Cet ahs *
* rae
ah /
H*hs =nwV)
V ah
eH(;
/ Cet arfrnet tehs *
* rae
eeec o h ah /
S*cntsl =nwVnic (V*hs )
V os ef
eR_on( S )ah ;
/ besit tepoe pcae*
* ls no h rpr akg /
RTA =s_ls(sl,g_tsp(cas 0))
EVL
vbes ef vsahv ls,
;
OTU:RTA
UPT EVL