Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
296 lines (276 sloc) 8.39 KB
\documentclass{beamer}
\usepackage{graphicx}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{tikz}
\usetikzlibrary{
positioning,
decorations.pathreplacing,
shapes.misc,
trees
}
\tikzset{file/.style={
chamfered rectangle,
chamfered rectangle corners=north west,
draw, thick,
fill=gray!30,
anchor=west
}}
\tikzset{folder/.style={
draw, thick,
fill=brown!50,
inner ysep=2mm,
anchor=west
}}
\usetheme{Madrid}
\setbeamercovered{transparent=50}
\title[Baby XS]{Baby XS: Just enough to get you started\\YAPC::NA 2012}
\author{Joel Berger}
\institute[UIC]{University of Illinois at Chicago}
\date{June 15, 2012}
\usepackage{pygments}
\providecommand{\subitem}[1]{\begin{itemize}\item#1\end{itemize}}
\providecommand{\code}[1]{{\texttt{\scriptsize{#1}}}}
\begin{document}
\begin{frame}
\maketitle
\end{frame}
\begin{frame}{What is XS?}
\begin{itemize}
\item XS the mechanism used to connect Perl and C
\begin{itemize}
\item write optimized functions
\item access \code{C} libraries
\end{itemize}
\end{itemize}
\vfill
\begin{tikzpicture}
\node (perl) [file,align=left] {\code{\#!/usr/bin/perl}\\\code{my \$var $=$ from\_c()}};
\node (xs) [right=of perl,align=center,draw,thick] {\Large XS\\?};
\node (c) [file,right=of xs,align=left] {\code{\#include ``headers''}\\\code{int from\_c () \{\ \ldots\ \}}};
\draw [thick,<-] (perl) -- (xs);
\draw [thick,->] (xs) -- (c);
\end{tikzpicture}
\vfill
\uncover<2->{But what \textit{is} XS?}
\begin{itemize}
\item<3-> XS is C functions/macros provided by Perl headers
\item<4-> XS is also XS-preprocessesor directives
\end{itemize}
\end{frame}
\begin{frame}{What are ``baby'' languages?}
\begin{block}{``Baby'' Langauge}
The naive code that new programmers write,\\a simple subset of the full language
\end{block}
\begin{itemize}
\item<2-> ``Baby Perl''
\begin{itemize}
\item<2-> no use of \code{map} / \code{grep}
\item<2-> avoids references
\item<2-> avoids \code{\$\_} and other special variables
\end{itemize}
\item<3-> ``Full XS'' is powerful but is lots to learn
\item<4-> ``Baby XS''
\begin{itemize}
\item<4-> looks like C
\item<4-> behaves like Perl
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}{Baby XS}
\begin{block}{What is Baby XS?}
Some ``easy'' idioms and rules-of-thumb to keep XS from becoming overwhelming
\end{block}
\begin{itemize}
\item<2-> ignores typemaps
\item<3-> uses Perl datatype-specific functions from \code{perldoc perlguts}
\item<4-> ignores most of the XSpp commands
\item<5-> uses a Perl-level function wrapper to munge input/output if needed
\begin{itemize}
\item<6-> ignores stack macros
\item<6-> avoids (most) issues of ``mortalization''
\end{itemize}
\item<7-> expands to ``real'' XS
\end{itemize}
\end{frame}
\begin{frame}{Types}
XS has types that are like their Perl counterparts
\begin{itemize}
\item Scalar $\Leftrightarrow$ \code{SV*}
\item Array $\Leftrightarrow$ \code{AV*}
\item Hash $\Leftrightarrow$ \code{HV*}
\end{itemize}
\uncover<2->{
Of course XS is really C so it also has types like
\begin{itemize}
\item \code{int}
\item \code{double}
\item \code{char*}
\end{itemize}
\ldots which Perl converts to/from \code{SV*} when used as arguments or return value
}
\begin{block}<3->{For Future Reference}
You can add you own automatic conversions via a \code{Typemap} file
\end{block}
\end{frame}
\begin{frame}{Sample XS File}
\scriptsize
\begin{columns}
\begin{column}{0.54\linewidth}
\begin{block}{}
\input{code/basic}
\end{block}
\end{column}
\begin{column}{0.44\linewidth}
\begin{tikzpicture}
\visible<3->{
\draw
[decoration={brace,amplitude=0.5em},decorate,ultra thick,gray]
(0,2.7) --
(0,1.7)
node [right=5mm,pos=0.5,align=left] {XS standard inclusions\\(loads standard C headers)}
;
}
\visible<4->{
\draw
[decoration={brace,amplitude=0.5em},decorate,ultra thick,gray]
(0,1.7) --
(0,0.2)
node [right=5mm,pos=0.5] {C Code}
;
}
\visible<2->{
\node at (1.4,-0.5) [gray] {Begin XS section};
}
\visible<5->{
\draw
[decoration={brace,amplitude=0.5em},decorate,ultra thick,gray]
(0,-1.2) --
(0,-2.9)
node [right=5mm,pos=0.5,align=left] {XS Code\\(Shown: Simple Declarations)}
;
}
\end{tikzpicture}
\end{column}
\end{columns}
\end{frame}
\begin{frame}{Using Scalars (\texttt{SV*}s)}
SV* behave like scalars in Perl, but have different actions based on type
\begin{columns}
\begin{column}{0.49\linewidth}
\begin{block}{Creating}
\begin{itemize}
\item \code{SV* newSViv(IV)}
\item \code{SV* newSVnv(double)}
\item \code{SV* newSVpvf(const char*, ...)}
\item \code{SV* newSVsv(SV*)}
\end{itemize}
\end{block}
\end{column}
\begin{column}{0.49\linewidth}
\begin{block}{Accessing}<2->
\begin{itemize}
\item \code{int SvIV(SV*)}
\item \code{double SvNV(SV*)}
\item \code{char* SvPV(SV*, STRLEN len)}
\item \code{char* SvPV\_nolen(SV*)}
\end{itemize}
\end{block}
\end{column}
\end{columns}
\begin{block}{Other Actions}<3->
Plenty of other Perl-like actions, see \code{perldoc perlguts} for more.
\begin{itemize}
\item \code{SvTRUE(SV*)} --- test for ``truth''
\item \code{sv\_catsv(SV*, SV*)} --- join strings
\end{itemize}
\end{block}
\end{frame}
\begin{frame}{Using Arrays (\texttt{AV*}s) and Multiple Returns}
\begin{itemize}
\item Perl-like functions, e.g. \code{av\_push}
\item<2-> filled with \code{SV*} objects
\item<3-> used as argument or return value, Perl uses references
\item<4-> ``mortalization'' problem for returns
\item<5-> recommended Baby XS way to return multiple values:
\end{itemize}
\vfill
\begin{block}{}
\scriptsize
\input{code/av}
\end{block}
\end{frame}
\begin{frame}{Sample Perl Module}
\begin{block}{}
\scriptsize
\input{code/module}
\end{block}
\begin{itemize}
\item Wrap C function calls in Perl subs
\begin{itemize}
\item munge inputs / outputs easier
\item abstraction if C function changes
\end{itemize}
\item Export the Perl function (if desired)
\end{itemize}
\end{frame}
\begin{frame}{Building/Packaging (Module::Build)}
\begin{columns}
\begin{column}{0.49\linewidth}
\begin{block}{Structure}
\begin{tikzpicture}
[
grow via three points={
one child at (0.7,-1) and
two children at (0.7,-1) and (0.7,-2)
},
edge from parent path={[thick] (\tikzparentnode.south) |- (\tikzchildnode.west)}
]
\node [folder] {root folder}
child { node [folder] {lib}
child { node [file] {MyModule.pm} }
child { node [file] {MyModule.xs} }
}
child [missing] {}
child [missing] {}
child { node [file] {Build.PL} }
child { node [file] { \ldots \vphantom{X}} }
;
\end{tikzpicture}
\end{block}
\vspace{4mm}
\end{column}
\begin{column}{0.49\linewidth}
\scriptsize
\begin{block}{Build.PL}
\input{code/build}
\end{block}
\end{column}
\end{columns}
\end{frame}
\begin{frame}{Other C Connections}
There are other mechanisms for hooking into C
\begin{itemize}
\item \code{Inline::C}
\begin{itemize}
\item write C in your Perl script
\item builds/loads the XS for you!
\item great for quick checks
\end{itemize}
\item \code{PDL::PP}
\begin{itemize}
\item part of \code{PDL}
\item special C interaction for fast numerical processing
\item has its own syntax
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}{Acknowledgements}
\includegraphics[width=0.7\linewidth]{uic}
\begin{itemize}
\item Graduate College Dean's Fellowship (Major Funding)
\item LAS Ph.D. Travel Award (Conference Funding)
\end{itemize}
\vfill
\url{https://github.com/jberger/YAPCNA2012}
\end{frame}
\end{document}