├── CPAN ├── .gitignore ├── Makefile ├── README ├── build │ └── latex.pl ├── chapters │ ├── 00-preamble.pod │ ├── 01-intro.pod │ ├── 10-oo.pod │ ├── 20-templates.pod │ ├── 30-web.pod │ ├── 40-database.pod │ ├── 50-xml.pod │ ├── 60-serialization.pod │ ├── 70-testing.pod │ ├── 80-protocols.pod │ └── 90-uncategorized.pod └── tex │ └── asbook.sty └── README /CPAN/.gitignore: -------------------------------------------------------------------------------- 1 | mynotes 2 | book.* 3 | cpan-chapter?.pdf 4 | -------------------------------------------------------------------------------- /CPAN/Makefile: -------------------------------------------------------------------------------- 1 | CHAPTERS=cpan-chapter1.pdf cpan-chapter2.pdf cpan-chapter3.pdf 2 | 3 | book.pdf: book.tex tex/asbook.sty 4 | TEXINPUTS=$$TEXINPUTS:tex xelatex book 5 | makeindex book 6 | TEXINPUTS=$$TEXINPUTS:tex xelatex book 7 | TEXINPUTS=$$TEXINPUTS:tex xelatex book 8 | 9 | book.tex: build/latex.pl chapters/*.pod 10 | build/latex.pl 11 | 12 | clean: 13 | find . -name '*~' -delete 14 | rm -f book.* *.log 15 | 16 | share: book.pdf $(CHAPTERS) 17 | @ rsync -aASPvz book.pdf ambs@eremita:public_html/cpan.pdf 18 | @ rsync -aASPvz $(CHAPTERS) ambs@camelo:public_html 19 | 20 | cpan-chapter1.pdf: book.pdf 21 | pdfjam -q book.pdf 7-14 -o cpan-chapter1.pdf 22 | 23 | cpan-chapter2.pdf: book.pdf 24 | pdfjam -q book.pdf 15-26 -o cpan-chapter2.pdf 25 | 26 | cpan-chapter3.pdf: book.pdf 27 | pdfjam -q book.pdf 27-44 -o cpan-chapter3.pdf 28 | 29 | nodraft: build/latex.pl chapters/*.pod tex/asbook.sty 30 | build/latex.pl -draft=0 31 | TEXINPUTS=$$TEXINPUTS:tex xelatex book 32 | makeindex book 33 | TEXINPUTS=$$TEXINPUTS:tex xelatex book 34 | TEXINPUTS=$$TEXINPUTS:tex xelatex book 35 | 36 | -------------------------------------------------------------------------------- /CPAN/README: -------------------------------------------------------------------------------- 1 | Book Guidelines 2 | -------------------------------- 3 | 4 | 1. Chapters should be independent 5 | 6 | A reader can read just a chapter, and no dependencies to other chapters 7 | should exist. Only pointers for further information. 8 | 9 | 2. Sections should be independent 10 | 11 | Only the first paragraphs of the chapter, before the first section, 12 | should/can compare the modules being discussed. Individual module sections 13 | should not make comparisons bwteeen modules unless strictly necessary. 14 | 15 | 3. See also section 16 | 17 | Each chapter should end in a special section named 'See also', that is 18 | an listing of modules available on CPAN for similar tasks, and a small 19 | description on them. 20 | 21 | 4. Depth 22 | 23 | No section should have more than 15 pages. I can accept a limit of 20 24 | for big frameworks like Catalyst. If it is impossible to explain the 25 | module in such a reduced number of pages, then the module should not 26 | be included (put it into the see also section, and point to other 27 | books/documentation). 28 | 29 | 5. Formatting 30 | 31 | Use C<...> for module names. Do not forget to add X and 32 | X whenever you mention a module (once per section, of course). 33 | 34 | Also, for footnotes, use N<< ... >> but do not leave a blank space 35 | before the N. If it gets glued with another word, the parser will 36 | detect it correctly. 37 | 38 | 6. Code blocks 39 | 40 | Perl code blocks should be enclosed in =begin Perl and =end 41 | Perl. Also, keep them indented two or three spaces. If referring to 42 | one line of code, only, use standard verbatim. For other non-perl 43 | blocks, use standard verbatim blocks. 44 | 45 | 46 | Build 47 | -------------------------------- 48 | 49 | ATM for building you need head from Pod::PseudoPod::LaTeX git repository. 50 | Check it out: git://github.com/chromatic/Pod-PseudoPod-LaTeX.git 51 | -------------------------------------------------------------------------------- /CPAN/build/latex.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -s 2 | 3 | use strict; 4 | use warnings; 5 | use utf8; 6 | 7 | use File::Slurp qw.edit_file.; 8 | use Pod::PseudoPod::LaTeX; 9 | 10 | our ($draft); 11 | 12 | $draft = 1 unless defined $draft; 13 | 14 | my $pod = 'build/book.pod'; 15 | my $tex = "book.tex"; 16 | 17 | 18 | my @files = sort { 19 | $a =~ m!(\d+)-! and my $c = $1; 20 | $b =~ m!(\d+)-! and my $d = $1; 21 | $c <=> $d 22 | } ; 23 | 24 | create_single_pod (pod => $pod, files => \@files); 25 | create_tex_from_pod (pod => $pod, tex => $tex); 26 | post_process_tex ($tex); 27 | 28 | unlink $pod; 29 | 30 | sub post_process_tex { 31 | my $filename = shift; 32 | edit_file { 33 | s/section\*/section/g; 34 | s/section\{\*/section*{/g; 35 | s/chapter\{\*/chapter*{/g; 36 | 37 | s/\\scriptsize\n\\begin{Verbatim/\\small\n\\begin{Verbatim/g; 38 | # this depends on the previous one 39 | s/ 40 | \\vspace{-6pt}\n(\\small\n\\begin{Verbatim}\[[^]]+frame=single[^]]+\]) 41 | / 42 | "\\vspace{-2pt}\n$1" # this way I can keep the indentation O:-) 43 | /xge; 44 | 45 | s!frame=single,label=!frame=single,numbers=left,label=!g; 46 | 47 | s!}``!}''!g; 48 | 49 | s/titleref/nameref/g; 50 | 51 | s!\bLaTeX\b!\\LaTeX{}!g; 52 | s!\bTeX\b!\\TeX{}!g; 53 | } $filename; 54 | } 55 | 56 | sub create_single_pod { 57 | my %data = @_; 58 | my $pod = $data{pod} or die; 59 | my @files = @{$data{files}} or die; 60 | 61 | open my $out, ">:utf8", $pod or die "Can't create file!"; 62 | 63 | for my $file (@files) { 64 | open X, "<:utf8", $file or die "Error opening file $!"; 65 | while() { 66 | print $out $_; 67 | } 68 | close X; 69 | } 70 | 71 | close $out; 72 | } 73 | 74 | sub parse_pod_file { 75 | my %data = @_; 76 | my $fh = $data{out} or die; 77 | my $pod = $data{pod} or die; 78 | my $parser = Pod::PseudoPod::LaTeX->new(captions_bellow => 1, keep_ligatures => 1); 79 | $parser->output_fh($fh); 80 | $parser->accept_target_as_text('CPANinfo', 'small', 'SeeAlso', 'right'); 81 | $parser->accept_target('Perl'); 82 | $parser->emit_environments('CPANinfo' => 'cpaninfo', 83 | 'small' => 'smallenv', 84 | 'right' => 'flushright', 85 | 'SeeAlso' => 'SeeAlso', 86 | 'Perl' => 'lstlisting'); 87 | $parser->parse_file($pod); 88 | } 89 | 90 | sub add_tex_preamble { 91 | my $fh = shift; 92 | # XXX - TODO: put this into a separate .tex file, and copy its 93 | # contents here. 94 | print $fh <<'EOTex'; 95 | \documentclass[a5paper,twoside,9pt]{extbook} 96 | EOTex 97 | print $fh "\\usepackage{draftwatermark}\n\\SetWatermarkLightness{0.95}\n" if $draft; 98 | print $fh <<'EOTeX'; 99 | \usepackage[left=1.7cm,right=2.2cm,top=2cm,bottom=1.5cm,footskip=7mm,headsep=7mm]{geometry} 100 | \usepackage{makeidx} 101 | \makeindex 102 | 103 | \usepackage{asbook} 104 | \setdefaultlanguage{english} 105 | 106 | \title{CPAN Modules and Frameworks} 107 | \subtitle{A bunch of relevant Perl modules and frameworks available from CPAN} 108 | \author{Alberto Simões\\[2mm]Nuno Carvalho} 109 | \date{First Edition} 110 | 111 | \begin{document} 112 | \frontmatter 113 | \maketitle 114 | 115 | EOTeX 116 | } 117 | 118 | sub add_tex_postamble { 119 | my $fh = shift; 120 | print $fh <<'EOTeX' 121 | \printindex 122 | \end{document} 123 | EOTeX 124 | } 125 | 126 | sub create_tex_from_pod { 127 | my %data = @_; 128 | my $tex = $data{tex} or die; 129 | my $pod = $data{pod} or die; 130 | open my $tex_fh, ">:utf8", $tex; 131 | add_tex_preamble $tex_fh; 132 | parse_pod_file out => $tex_fh, pod => $pod; 133 | add_tex_postamble $tex_fh; 134 | close $tex_fh; 135 | 136 | } 137 | -------------------------------------------------------------------------------- /CPAN/chapters/00-preamble.pod: -------------------------------------------------------------------------------- 1 | 2 | =for latex 3 | 4 | \parskip 2mm 5 | 6 | =end 7 | 8 | =head0 *Preamble 9 | 10 | This book does not intend to be a complete reference to all modules 11 | from CPAN, nor a complete reference to some modules from 12 | CPAN. That would be mostly impossible to print, as there are thousands 13 | of Perl modules available, and some of them have rather long 14 | documentation. 15 | 16 | That said, B<< this book aims to introduce a set of useful modules and 17 | frameworks >> that are not easy to learn just by reading the main 18 | Module documentation file (the one you usually get when executing 19 | C on the command line). Do not expect that all 20 | features are presented. The more important were chosen, and for 21 | further details you should read each module documentation. 22 | 23 | The book is split in different chapters, each one covering a specific 24 | type of modules or frameworks. For instance, we have a chapter on web 25 | frameworks, and a chapter on XML parsers. For each chapter you will 26 | get a brief explanation or comparison between the modules that will be 27 | described, together with the reason why those modules where 28 | chosen. Then, each section will focus in one of those modules. And the 29 | chapter ends with a special section, where pointers to other useful 30 | modules that were not discussed but might be useful. 31 | 32 | When writing this book we tried to make it possible to be read in any 33 | order: you can peek at a chapter about a topic you are interested in, and 34 | read it without reading all the other chapters. We even tried to make 35 | sections independent, so you can read a section on a specific 36 | framework without needing to read the sections for the other 37 | frameworks. Nevertheless, it might be a good idea to read the 38 | introduction to that chapter before reading a specific section. 39 | 40 | If you are new to Perl or to using modules from CPAN we suggest you 41 | start by reading our first chapter. It presents some of the history 42 | about CPAN, how it works, who contributes code, how you can install 43 | modules from CPAN in your computer and where you can get information 44 | on how to contribute to CPAN. 45 | 46 | Regarding previous knowledge on Perl or CPAN, we expect you to know 47 | how to write basic Perl programs, including the use of modules and 48 | references. The knowledge about I Object Oriented Perl might be 49 | useful as well. 50 | 51 | The authors would like to thank 52 | Stevan Little, 53 | Pedro Melo, 54 | David Precious, 55 | Marcos Ramos, 56 | Magda Joana Silva 57 | and 58 | Sawyer X 59 | for they comments, suggestions and proofreading. 60 | 61 | Also, this book wouldn't be possible without LaTeX and the 62 | C Perl module from chromatic and Moritz. 63 | 64 | =begin right 65 | 66 | I 67 | 68 | =end right 69 | 70 | =for latex 71 | 72 | \parskip 0mm 73 | \tableofcontents 74 | \mainmatter 75 | \parskip 2mm 76 | \pagestyle{fancy} 77 | 78 | =end 79 | 80 | =cut 81 | 82 | ## Local Variables: 83 | ## ispell-local-dictionary: "english" 84 | ## mode: flyspell 85 | ## End: 86 | -------------------------------------------------------------------------------- /CPAN/chapters/01-intro.pod: -------------------------------------------------------------------------------- 1 | 2 | =head0 The Comprehensive Perl Archive Network 3 | 4 | X The Comprehensive Perl Archive Network, known as CPANN<< It is 5 | named after another archive, The Comprehensive TeX Archive Network, an 6 | archive of TeX and LaTeX modules and styles. >> is an archive of Perl 7 | applications, modules and frameworks. It is available through the 8 | Network and is Comprehensive, in the sense that it is a large 9 | collection, but also that it includes nearly all developed modules, 10 | and they are available in an organized way. 11 | 12 | Note that CPAN does not have any kind of guarantee that the available 13 | modules are not malicious, or that provides the documented 14 | features. Therefore, knowing what modules are being used by the 15 | community is great, as it gives information about their quality and 16 | trust. 17 | 18 | CPAN is available from L or one of the hundreds of 19 | available mirrors around the world. You can access the modules using 20 | C, C or, more interestingly, using the search feature of 21 | CPAN. It will give you information on the module author, what the 22 | latest version is, points to the download file, gives you access to 23 | the module documentation and a rating system, where users can rate modules. 24 | 25 | There are a bunch of other web sites that were developed to make the 26 | CPAN experience richer. There are some of them: 27 | 28 | =over 29 | 30 | =item B 31 | 32 | X 33 | AnnoCPAN (L) gives you access to modules 34 | documentation, but also to annotations over that documentation. What 35 | this means, is that any user can annotate the documentation with 36 | further information, incoherences and other (relevant) information. 37 | 38 | =item B 39 | 40 | X 41 | CPAN Forum (L) offers a forum for each 42 | module. Some authors maintain their forum active, some others do not 43 | use them. Some others, prefer to create forums in other web sites, or 44 | to create mailing lists. 45 | 46 | =item B 47 | 48 | X 49 | CPAN Testers (L) gives information 50 | about how each module installs, and how their tests pass or fail in 51 | different architectures and perl versions. When installing a module, 52 | if the build fails, this web site can be used to detect whether it is 53 | a local fail, or a known bug. For module developers it is very valuable 54 | to check if modules are portable and if tests pass on different systems, helping 55 | all authors to improve the quality and reliability of their code. 56 | 57 | =item B 58 | 59 | X 60 | CPAN Ratings (L) allows users of modules to 61 | leave ratings and reviews on Perl modules, providing useful information to 62 | other users looking for a module to fulfil a particular task and facing 63 | multiple modules to decide between. Ratings are visible when viewing 64 | distributions on L. 65 | 66 | =item B 67 | 68 | X MetaCPAN (L) is a new 69 | interface to the CPAN repository. Aims at a fresh, light and fast user 70 | interface, using new technologies like JavaScript. It has a very nice 71 | feature over the standard CPAN web site, as it indexes new modules 72 | faster. They get available on MetaCPAN a few minutes after the 73 | developer uploads them. 74 | 75 | =back 76 | 77 | =head1 Installing CPAN Modules 78 | 79 | X There are three main different ways to install a 80 | module from CPAN: using an automated installation tool, like C, 81 | C or C, installing a package provided by your OS vendor, 82 | or installing manually. Installing packages from the OS vendor's 83 | repository (for instance, via C for Debian-based systems) is 84 | preferred by some, but packages can lag behind the latest versions 85 | available on CPAN. 86 | 87 | Installing modules using CPAN.pm (C), CPANPLUS (C) or 88 | cpanminus (C) is considered ideal by many, as it provides easy 89 | installation with automated tests and dependency handling, and allows 90 | you to install the freshest version available from CPAN. 91 | 92 | Installing manually gives the most control, but forces you to deal 93 | with satisfying dependencies yourself; for a module with a long 94 | dependency chain on a fresh Perl install, this can be a real pain. 95 | 96 | =head2 *Using C 97 | 98 | X X 99 | The command line tool C ships with Perl, and can be described as 100 | the official tool to install modules. The only situations whether you 101 | probably do not have C installed in your system are derived of 102 | using ActiveState ActivePerl (in this case read the section on 103 | C), or using a Linux distribution that splits the Perl package in 104 | smaller packages. In this case use your package system and search for 105 | the C package. 106 | 107 | Usually you want to run C as a system administrator, so you have 108 | the needed permissions to write into the system tree. Alternatively, the CPAN 109 | client can run as a normal user, and be configured to use C where required 110 | to perform the final installation; this has the benefit of making the test suite 111 | run as a normal user, which can be preferable for security. 112 | 113 | Recent C 114 | versions have an automated configuring system. Nevertheless, if you 115 | run C and it asks a lot of questions, you are safe on choosing 116 | the default answers. Just take care to choose a nearby mirror, so 117 | packages download faster. 118 | 119 | C can be used from the command line issuing: 120 | 121 | cpan Module::Name Another::Module 122 | 123 | This will install in your system the two modules C and 124 | C. The other approach is using C shell. For 125 | that, just type C in your command line, and a C shell will 126 | appear. You can get help on the C shell commands using "C". To 127 | install modules use the "C" command. 128 | 129 | If for some reason the C command is not installed, you can try to execute 130 | the CPAN.pm shell with: 131 | 132 | perl -MCPAN -e shell 133 | 134 | Z 135 | 136 | =begin screen Simple cpan interaction 137 | 138 | cpan shell -- CPAN exploration and modules installation 139 | Enter 'h' for help. 140 | 141 | cpan[1]> i Module::Name Another::Module 142 | 143 | =end screen 144 | 145 | =head2 *Using C 146 | 147 | X X X 148 | C is the command line tool to the C module, that 149 | enhances the C module used by C with some new 150 | functionalities. Although some C commands differ from the ones 151 | available on C, the basic behavior is similar. To install from 152 | the command line add the "C<-i>" switch: 153 | 154 | cpanp -i Module::Name Another::Module 155 | 156 | For the interactive shell, C also uses the C command (see 157 | A). 158 | 159 | =head2 *Using C 160 | 161 | X X C is available through 162 | X C module. The main advantage of 163 | C regarding the other two approaches described above is 164 | efficiency. C is very quick, as it doesn't perform the amount 165 | of checks C or C does, and finds out about modules via 166 | the CPAN MetaDB API, rather than by parsing large indexes. This 167 | provides fast operation, and ensures fresh module versions. 168 | 169 | Install the module, and use C from the command lineN<< 170 | C does not have an interactive shell. You must use it directly 171 | from the command line. >>. 172 | 173 | cpanm --sudo Module::Name another::Module 174 | 175 | The "C<--sudo>" switch is used to ask C to call C when 176 | installing the module. This way you can call C from your user, 177 | knowing the when needed a password will be asked. 178 | 179 | =head2 *ActivePerl PPM 180 | 181 | X X PPM stands for I, and it is 182 | an application by ActiveState to install modules from their own module 183 | repository. These packages have the advantage of being pre-compiled, 184 | making it easier to install on Windows machines without a compiler 185 | available. The main drawback is that PPM repositories do not have all 186 | available modules from CPAN, so you may well find that the module you 187 | need is not available, or is somewhat outdated. 188 | 189 | Therefore, the authors suggest Windows users to download the Strawberry 190 | Perl X distribution which includes C and a quite 191 | complete build system. 192 | 193 | =head2 *Manual Install 194 | 195 | X The process of installing a module by 196 | hand depends a little on your operating system. The instructions 197 | presented here describe the most usual process. First, get the module 198 | archive from a CPAN mirror. These files are usually named I, 199 | and are compressed using C and archived by C. For this 200 | example we will describe the process of installing C: 201 | 202 | =begin screen Manual installation of a Perl Module 203 | 204 | $ tar zxf Try-Tiny-0.09.tar.gz 205 | $ cd Try-Tiny-0.09 206 | $ perl Makefile.PL 207 | Checking if your kit is complete... 208 | Looks good 209 | Writing Makefile for Try::Tiny 210 | Writing MYMETA.yml 211 | $ make 212 | cp lib/Try/Tiny.pm blib/lib/Try/Tiny.pm 213 | Manifying blib/man3/Try::Tiny.3 214 | $ make test 215 | PERL_DL_NONLAZY=1 /opt/local/bin/perl [...] t/*.t 216 | t/basic.t ....... ok 217 | t/finally.t ..... ok 218 | t/given_when.t .. ok 219 | t/when.t ........ ok 220 | All tests successful. 221 | Files=4, Tests=48, 0 wallclock secs 222 | ( 0.06 usr 0.02 sys + 0.14 cusr 0.03 csys = 0.25 CPU) 223 | Result: PASS 224 | $ sudo make install 225 | [...] 226 | Appending installation info to /opt/local/lib/.../perllocal.pod 227 | $ cd .. 228 | $ rm -fr Try-Tiny-0.09* 229 | 230 | =end screen 231 | 232 | A brief explanation of the issued commands follows: 233 | 234 | =begin small 235 | 236 | =over 237 | 238 | =item * 239 | 240 | B uncompress the tarball. The command used is the more common 241 | for most Unix/BSD systems. It might be similar under Windows if you 242 | have GNU tools installed. If not, you can download a tool to 243 | uncompress these kind of files, like C<7zip> or C. 244 | 245 | =item * 246 | 247 | B go to the folder created after uncompressing the tarball. 248 | 249 | =item * 250 | 251 | B run C against the C file, to configure the 252 | module. Note that some modules use a different build system, named 253 | C X X instead of 254 | C X 255 | X, the one responsible for C. In 256 | this case, you should run C against the C file: 257 | 258 | perl Build.PL 259 | 260 | =item * 261 | 262 | B this step is the process of building the module files. When the module 263 | is just composed by Perl modules, this step just copy some files from 264 | different places to a temporary staging folder named C. If the 265 | module includes C code, this step will also compile it. 266 | 267 | Depending on your system C might be named C or 268 | C. Also, if in the previous step you needed to use C, 269 | then instead of C you should run the newly created C 270 | file. 271 | 272 | =item * 273 | 274 | B after compiling the module it is a good idea to test if 275 | your module is passing all the tests defined by the developers. To 276 | test your newly compiled module run C<< make test >> or C<< ./Build test 277 | >> depending on the module you are installing. 278 | 279 | =item * 280 | 281 | B finally, time to install the module. Just run C<< make 282 | install >> or C<< ./Build install >>. It might be a good idea to run 283 | this command as superuser (for example, prepending C to the 284 | command) as, by default, modules are installed system-wide into a directory to 285 | which normal users will not have write access.. 286 | 287 | =back 288 | 289 | =end small 290 | 291 | =head1 Installing Modules Locally 292 | 293 | Perl lets you install modules on non standard directories. This is 294 | relevant if you are not the system administrator and need to install 295 | modules in your home directory, of if you like to have some modules 296 | installed on different paths for different projects. 297 | 298 | =head2 *Configuring CPAN for a Non-Standard Path 299 | 300 | When using the usual CPAN clients (C and C) it is 301 | possible to define some variables where the modules will be installed. 302 | Enter the CPAN shell and configure the variables C and 303 | C. They are the default arguments when running 304 | C or C. The following screen shows the 305 | interaction with C to set the installation base to 306 | C. 307 | 308 | =begin screen Configuring CPAN install base 309 | 310 | $ cpan 311 | cpan> o conf makepl_arg INSTALL_BASE=/home/user/mymodules 312 | cpan> o conf mbuild_arg "--install_base /home/user/mymodules" 313 | cpan> o conf commit 314 | 315 | =end screen 316 | 317 | After this, every time you run the C shell, these configuration 318 | variables will be loaded, and will be used when installing modules. You 319 | will not need to configure the client again. 320 | 321 | To use the modules installed in this location you have two 322 | options. The first is to define an environment variable named 323 | CX with the path where your modules got installed: 324 | 325 | export PERL5LIB=/home/user/mymodules 326 | 327 | Unfortunately the Perl 5 installation path is quite complicated, and 328 | probably you will need to define more than one path: 329 | 330 | =begin small 331 | 332 | export PERL5LIB=/home/user/mymodules:/home/user/mymodules/5.14.0/: 333 | /home/user/mymodules/site_perl/5.14.0/:/home/user/mymodules/site_perl/ 334 | 335 | =end small 336 | 337 | A more interesting and versatile method is to use the C pragma in 338 | the beginning of all your script files, followed by the path names 339 | where to find modules: 340 | 341 | use lib '/home/user/mymodules'; 342 | 343 | =head2 *Using C Module 344 | 345 | X X 346 | 347 | The module C makes it easier to have a module tree in your 348 | home (by default, C<~/perl5>), install modules and use them. To start 349 | with, you need C in your system. If your system doesn't 350 | have this module installed and your administrator is not willing to 351 | install it, you can install it using the I technique. 352 | 353 | First, download the C tarball from a CPAN mirror. Extract 354 | it, and run: 355 | 356 | perl Makefile.PL --bootstrap 357 | 358 | The system should ask you some questions. You can let it configure as 359 | much as possible automatically. Then, run: 360 | 361 | make test && make install 362 | 363 | After this, you need to set up your environment variables. You can the 364 | list of environment variables to define running: 365 | 366 | perl -I$HOME/perl5/lib/perl5 -Mlocal::lib 367 | 368 | Add the output of this command to your C<~/.bashrc> or your 369 | C<~/.cshrc> file, depending on the shell you are running (Bourne shell 370 | or C shell, respectively). After this step you might need to reload 371 | the config file (or close and open a new shell). 372 | 373 | To install modules use one of the following command lines: 374 | 375 | cpan -I Module::Name 376 | cpanm -l Module::Name 377 | 378 | As some of these commands just works on recent versions, you can use 379 | instead: 380 | 381 | perl -MCPAN -Mlocal::lib -e 'CPAN::install("Module::Name")' 382 | 383 | Now, you can write your scripts adding the following line in the 384 | beginning of your file: 385 | 386 | use local::lib; 387 | 388 | If you prefer to use the C pragma, you can use: 389 | 390 | use lib '/home/user/perl5'; 391 | 392 | =head2 *Using App::perlbrew 393 | 394 | X X 395 | 396 | C is something completely different. Its main purpose 397 | is not to install Perl modules locally, but to install a B Perl 398 | version locally. First of all, install the module. Although you can 399 | install the module like a standard one, the author defends that the 400 | best installation procedure is to execute the following command on 401 | your shell: 402 | 403 | curl -L http://xrl.us/perlbrewinstall | bash 404 | 405 | This will install C in your home folder, under 406 | C<~/perl5/perlbrew/bin>. After installation, follow the on-screen 407 | instructions to set up your C<.bashrc> or C<.cshrc> to define the 408 | correct environment variables. 409 | 410 | After this you need to reload your shell configuration files or start 411 | a new shell session. To initialize the C folders run: 412 | 413 | perlbrew init 414 | 415 | Then, a good idea is to install a local C to your C 416 | installation. This can be easily done by: 417 | 418 | perlbrew install-cpanm 419 | 420 | And now everything is set to start using C. 421 | 422 | =over 423 | 424 | =item B 425 | 426 | C is great if you need more than one Perl version installed 427 | in your system. For example, because if you are a module developer and 428 | want to test your software against different Perl versions, or because 429 | you have different Perl projects that depend on different Perl 430 | versions. 431 | 432 | To install a Perl version using C is simple. Just issue the 433 | command: 434 | 435 | perlbrew install perl-5.14.0 436 | 437 | You just need to know the official version name. If everything goes 438 | well you should end up with a new Perl installed in your home. For 439 | Perl 5.14.0, it will get installed on (using default the configuration 440 | values): 441 | 442 | ~/perl5/perlbrew/perls/perl-5.14.0/bin/perl 443 | 444 | =item B 445 | 446 | Having more than on Perl installed in the system it gets crucial to 447 | know what Perl versions are installed, and to switch from different 448 | Perl versions. A list of the Perl versions installed in your system is 449 | available using: 450 | 451 | perlbrew list 452 | 453 | It lists the name or the path to the Perl interpreter, and its 454 | versions. The current Perl version is marked with an asterisk. You can 455 | use a Perl version temporarily for the current shell with: 456 | 457 | perlbrew use perl-5.14.0 458 | 459 | All consequent commands will use that Perl version, but any other 460 | shell or new shell will still use the previous Perl interpreter. 461 | To switch to other Perl interpreter globally (for your user) use: 462 | 463 | perlbrew switch perl-5.14.0 464 | 465 | =item B 466 | 467 | The modules installation is simple. Switch to the Perl where you want 468 | to install the module, and use C: 469 | 470 | cpanm Module::Name 471 | 472 | The module will be installed in the tree for that specific Perl. This 473 | means that every time you switch from Perl version an installed module 474 | can be no longer available. 475 | 476 | =back 477 | 478 | Although C is quite interesting and powerful, it is still 479 | under heavy development and has some known problems. To use it well 480 | you need some expertise. For further documentation check the 481 | C page. 482 | 483 | =cut 484 | 485 | ## Local Variables: 486 | ## ispell-local-dictionary: "english" 487 | ## mode: flyspell 488 | ## End: 489 | -------------------------------------------------------------------------------- /CPAN/chapters/10-oo.pod: -------------------------------------------------------------------------------- 1 | 2 | =head0 Modern Perl OO 3 | 4 | Z 5 | 6 | XX XX XX 7 | X Perl always suffered from the lack of a rigid object 8 | oriented system. Although blessed references did the trick for years, 9 | developers felt sorry they did not have support for some common OO 10 | features, like interfaces (or roles), multiple inheritance and 11 | encapsulation. 12 | 13 | A few years ago C appeared. It revolutionized the OO world of 14 | Perl. During these years it has been enriched with a lot of relevant 15 | and useful features. 16 | 17 | Nevertheless, C still imposes a delay upon initial compilation 18 | and startup, which can be problematic for applications like CGI 19 | scripts, which are parsed and run every time a request is made, for 20 | which the overhead of loading C may not be acceptable. 21 | 22 | To minimize this problem some developers started C, a smaller 23 | implementation that does not include all C functionalities, but 24 | only the more relevant ones. It maintains the API compatibility with 25 | C, making it easier to switch whenever needed. 26 | 27 | Another implementation, named C, claims to be yet smaller and 28 | faster than C, although at the time of writing, it still has a relatively 29 | small userbase in comparison. 30 | 31 | Given that all these modules share the same basic syntax, this chapter 32 | structure will be slightly different from the next ones. First, we 33 | will present C, then, what C offers over C, and 34 | finally, what C offers in addition. It is hoped that this approach 35 | will make this chapter smaller and more helpful to the reader when choosing one 36 | implementation. 37 | 38 | =head1 Moo 39 | 40 | XX 41 | 42 | =begin CPANinfo 43 | 44 | B 0.009008 45 | 46 | B L 47 | 48 | =end CPANinfo 49 | 50 | C uses the usual approach to define objects: use packages (the 51 | Perl equivalent to what we are used to call classes). 52 | 53 | =head2 *Attributes 54 | 55 | There follows a simple example of a I object with two attributes, 56 | I and I. The first is a read only attribute (you can't 57 | change the person's name after initializing the object) while the second 58 | has read/write permissions. This information is used by C to 59 | define whether to create accessors that are able to set and get 60 | values, or just get values. The I option means that every 61 | time a I object is created a name must be providedN<< All 62 | packages in Perl should return a true value after successful 63 | load. That is why they usually have a last line with C<1;>. In the 64 | following examples we will add that line every time the example is a 65 | full package. >>. 66 | 67 | =begin Perl 68 | 69 | package Person; 70 | use Moo; 71 | 72 | has name => ( is => 'ro', required => 1 ); 73 | has phone => ( is => 'rw' ); 74 | 1; 75 | 76 | =end Perl 77 | 78 | Note that the "fat comma" arrow is equivalent to a comma, and the parenthesis 79 | are not needed. The first attribute could be defined as: 80 | 81 | has "name", "is", "ro", "required", 1; 82 | 83 | but its legibility is not the same. Therefore we will stick to the 84 | usage of the fat comma arrow together with parenthesis or indentation to 85 | group attribute options. 86 | 87 | This object usage is similar to the usage of standard blessed 88 | reference objects. C creates the constructor and accessors automatically 89 | for you. 90 | 91 | =begin Perl 92 | 93 | use Person; 94 | 95 | my $person = Person->new(name => 'John'); 96 | print $person->name; 97 | 98 | $person->phone("93453423"); 99 | print $person->phone; 100 | 101 | =end Perl 102 | 103 | Unlike the next two alternatives, C doesn't have a type 104 | system. If you need to guarantee that an attribute keeps a specific 105 | type of data, you need to validate it yourself. That is done by adding a 106 | code reference. The next example is a redefinition of the attribute 107 | C requiring the value to consist of nine numeric digits: 108 | 109 | =begin Perl 110 | 111 | has phone => 112 | is => 'rw', 113 | isa => sub { 114 | die "Need a nine digit string!" unless $_[0] =~ /^\d{9}$/ 115 | }; 116 | 117 | =end Perl 118 | 119 | Attributes can have default values. This is specified using the 120 | C option. It takes a code reference that receives the object, 121 | and should return the new value. Note that you should not rely on 122 | other attributes for this initialization as there is no guarantee that 123 | they were already populated (for that use I attributes). As an 124 | example, you can create an attribute C and define it 125 | automatically to I if not specified. 126 | 127 | =begin Perl 128 | 129 | has gender => 130 | is => 'ro', 131 | default => sub { return 'female' }; 132 | 133 | =end Perl 134 | 135 | If the default value needs to be computed based on other attributes, or 136 | the task of computing it is time or CPU expensive, you can define the 137 | attribute as I (set it to a true value). This way the value will 138 | only be computed when it is first accessed. 139 | 140 | When some code should run after a specific attribute is set a 141 | C should be used. It is just a code reference that will be 142 | called over the object (receiving the object as the first parameter), with the 143 | new value as the second param. For instance, when a phone number is registered, 144 | call some function to send a SMS to the new number acknowledging the change: 145 | 146 | =begin Perl 147 | 148 | has phone => 149 | is => 'rw', 150 | isa => sub { 151 | die "Need a nine digit string!" unless $_[0] =~ /^\d{9}$/ 152 | }, 153 | trigger => sub { 154 | my ($self, $new_number) = @_; 155 | send_sms( to => $new_number, 156 | msg => "Hello there " . $self->name 157 | . ", phone number correctly updated, thanks!") 158 | }; 159 | 160 | =end Perl 161 | 162 | =head2 *Inheritance and Roles 163 | 164 | The usual inheritance between classes is also possible. In fact, 165 | C supports multiple super-classes. They are defined with the 166 | C methodN<< Calling C more than once will B 167 | the super-classes, not add a new. >>: 168 | 169 | =begin Perl 170 | 171 | package Student; 172 | use Moo; 173 | extends 'Person'; 174 | 175 | has final_grade => ( is => 'rw' ); 176 | 1; 177 | 178 | =end Perl 179 | 180 | One of C's sacrifices to remain small is to not support 181 | C. This means it is not that easy to override 182 | methods. Nevertheless it can be done using method modifiers (see 183 | A). 184 | 185 | If the code being extended (the super-class) will never be 186 | instantiated, probably you want to use I. I can be seen 187 | as the Java interfaces, or as a specific set of methods that implement 188 | a specific functionality. C roles are defined using the 189 | C X X module. 190 | 191 | The definition of a role is simple. Create a brand new package, use 192 | C, and write all the code the role should implement. As a 193 | simple example consider a role that dumps the object to the standard 194 | error (useful for debugging): 195 | 196 | =begin Perl 197 | 198 | package Role::Dumper; 199 | use Moo; 200 | use Role::Tiny; 201 | use Data::Dumper; 202 | 203 | sub dump { 204 | my $self = shift; 205 | print STDERR Dumper($self); 206 | } 207 | 208 | 1; 209 | 210 | =end Perl 211 | 212 | Now, you can switch from role to role using C. For simplicity 213 | C activates only one role at a time. Our class now can call 214 | C on a C: 215 | 216 | =begin Perl 217 | 218 | package Person; 219 | use Moo; 220 | 221 | ... 222 | 223 | my $person = Person->new(name => 'Mary', phone => '987654321'); 224 | 225 | with 'Role::Dumper'; 226 | $person->dump; 227 | 228 | =end Perl 229 | 230 | Note that the C method is not implemented in the I 231 | class, but rather in the role. This means you can activate this role 232 | for any object. 233 | 234 | =head2 *Method Modifiers 235 | 236 | ZXX 237 | 238 | The method modifiers described here are not really part of the object 239 | system, but are available in C as a workaround for some 240 | limitations, like the lack of access to C. They are implemented 241 | though the C module that can be used 242 | independently. 243 | 244 | These methods can be viewed as hooks that are called before and/or 245 | after some methods are called. As an example, we want to add a line of 246 | debug before dumping a person. Therefore, in the C file we 247 | will switch to the role that implements C, and activate a 248 | modifier, asking to run some code before the actual method: 249 | 250 | =begin Perl 251 | 252 | with 'Role::Dumper'; 253 | before dump => sub { print STDERR "Dumping a Person!\n" }; 254 | 255 | =end Perl 256 | 257 | The method will receive the same arguments as the method 258 | itself. Return values are ignoredN<< In fact, you can can change C<@_> 259 | directly, and the changed values will be received by the original 260 | method. But this approach is not suggested. Use C instead. >>. 261 | 262 | C works in the same way, and receives C<@_> as the original 263 | method received. This means that if the original method changes the 264 | values, C will receive them modified. 265 | 266 | Finally, there is C, that is called instead of the original 267 | method. This means that the code run should call the original method 268 | (if needed, or just ignore it). For that to be possible, the code you 269 | define will receive as first argument the method that is being 270 | replaced. Suppose you have a role that implements a method similar 271 | to that described above, to send a SMS message, but which uses the 272 | phone number stored in the object, but you need to add a prefix to the 273 | number ("+0", for instance) before calling it, and that you need to 274 | get the result (a boolean) and transform it into a string "OK" or 275 | "NOK": 276 | 277 | =begin Perl 278 | 279 | with 'SMS::Role'; 280 | around send_sms => sub { 281 | my ($orig, $self, $message) = @_; 282 | 283 | my $number = $self->phone; # save number 284 | $self->phone("+0$number"); 285 | my $ok = $self->$orig($message); 286 | $self->phone($number); # revert number 287 | return $ok ? "OK" : "NOK"; 288 | } 289 | 290 | =end Perl 291 | 292 | =head1 Mouse 293 | 294 | XX 295 | 296 | =begin CPANinfo 297 | 298 | B 0.93 299 | 300 | B L 301 | 302 | =end CPANinfo 303 | 304 | As mentioned in this chapter introduction C aims to be a subset 305 | implementation of C, with fewer dependencies, with an optional 306 | XS back-end (meaning you will not necessarily need a C<< C >> 307 | compiler) and reducing the C startup time. 308 | 309 | =head2 *Attributes 310 | 311 | The syntax for defining attributes is the same C uses. The first 312 | main difference is that C introduces a type system, instead of 313 | the code reference that C uses to check values types. Some of 314 | C data types are: C to store anything, C for boolean 315 | values, C to store a string or a number, just like Perl scalar 316 | variables handle them, C for numbers, including reals, C for 317 | integer numbers, C when you are storing a object class 318 | name, C for any kind of reference, C for a reference 319 | to a scalar, C to a reference to an array, C to a 320 | reference to hashes, C for regular expressions, etc. You 321 | can get a detailed list and their relationship in the 322 | C. This 323 | module also allows you to define sub-types based on the built-in 324 | types. Finally, if you define a type name that is unknown, C 325 | will interpret it as the name of a class that should be checked using 326 | C. 327 | 328 | The C value for the attribute can be set like with C 329 | using a code reference, but it is also possible to specify directly a 330 | scalar value (you can not use references). 331 | 332 | Our I class is written with C as follows: 333 | 334 | =begin Perl 335 | 336 | package Person; 337 | use Mouse; 338 | 339 | has name => ( 340 | is => 'ro', required => 1, isa => 'Str', 341 | ); 342 | 343 | has phone => ( 344 | is => 'rw', 345 | isa => 'Str', 346 | trigger => sub { 347 | send_sms( to => $_[1], 348 | msg => "Phone number correctly updated.") 349 | }, 350 | ); 351 | 352 | has gender => ( 353 | is => 'ro', default => "female", 354 | ); 355 | 356 | 1; 357 | 358 | =end Perl 359 | 360 | There are some relevant differences in this code. First, we are not 361 | requiring the phone number to have nine digits. Check 362 | A for a solution through subtypes. The other 363 | difference is more subtle: the C method receives the same 364 | first two arguments as the C example, but a third argument is 365 | also supplied: the value before the attribution takes place. 366 | 367 | =head2 *Inheritance and Roles 368 | 369 | Inheritance in C is also handled using the C method. 370 | You an redefine completely any attribute: 371 | 372 | has name => ( is => 'rw', isa => 'Str' ); 373 | 374 | Or you can use the superclass attribute definition as the base for 375 | your new definition. If the line above this paragraph was in a 376 | superclass, we can redefine the attribute as required with: 377 | 378 | has +name => ( required => 1 ); 379 | 380 | This would maintain the C and C properties, and add the 381 | C flag. 382 | 383 | Regarding roles (handled by C X X), they are very similar in behavior with C roles. 385 | 386 | The main difference is that a C can require that the 387 | object that uses it implements some methods. That is specified with 388 | the C method. As an example, consider a role that adds 389 | arithmetic to any object that can be converted to a real value. For 390 | that to be possible, the object must implement a method named 391 | C. The role will use this method to perform the arithmetic 392 | operation. 393 | 394 | =begin Perl 395 | 396 | package Arithmetics; 397 | use Mouse::Role; 398 | 399 | requires 'value'; 400 | 401 | sub add { 402 | my ($self, $other) = @_; 403 | return $self->value + $other->value; 404 | } 405 | 406 | 1; 407 | 408 | =end Perl 409 | 410 | Now, an object which represents stock exchange values can add stock 411 | values together: 412 | 413 | =begin Perl 414 | 415 | package Stocks; 416 | use Mouse; 417 | 418 | has name => ( is => 'rw', isa => 'Str' ); 419 | has qt => ( is => 'rw', isa => 'Int' ); 420 | has price => ( is => 'rw', isa => 'Num' ); 421 | 422 | sub value { 423 | my $self = shift; 424 | return $self->qt * $self->price; 425 | } 426 | 427 | with 'Arithmetics'; 428 | 429 | my $stock1 = Stocks->new( qt => 10, price => 1.1 ); 430 | my $stock2 = Stocks->new( qt => 5, price => 4.3 ); 431 | my $total_amount = $stock1->add($stock2); 432 | 433 | 1; 434 | 435 | =end Perl 436 | 437 | Note that you can use an accessor to satisfy the role 438 | C. This object that represents a product and its market 439 | value would work as well: 440 | 441 | =begin Perl 442 | 443 | package Product; 444 | use Mouse; 445 | 446 | has name => ( is => 'rw', isa => 'Str' ); 447 | has value => ( is => 'rw', isa => 'Int' ); 448 | 449 | with 'Arithmetics'; 450 | 451 | my $prod1 = Product->new( value => 3 ); 452 | my $prod2 = Product->new( value => 5 ); 453 | my $total_prods = $prod1->add($prod2); 454 | 455 | 1; 456 | 457 | =end Perl 458 | 459 | You just need to make sure that the line with the C that defines 460 | the attribute which accessor will satisfy the requirements is defined 461 | before composing the role. With this role you can even add different 462 | type of classes, given they satisfy the C role. 463 | 464 | As a final note, note that unlike C, C supports more than 465 | one role at a time. 466 | 467 | =head2 *Method Modifiers 468 | 469 | C supports C, C and C, just like C 470 | The main difference is that they accept a regular expression, and will 471 | apply the modifier to all the methods that match the expression. 472 | 473 | =head2 *Type Constraints 474 | 475 | Z X X 477 | 478 | To have built-in types is a cool feature, but can be limiting. If you 479 | compare our I example using C and C, you will 480 | notice that C restricts phone numbers to have only a fixed amount 481 | digits while C accepts any string. 482 | 483 | The solution to make C more picky about its values is to define 484 | new data types. This is performed using the 485 | C module, that defines new data types 486 | based on C basic types. 487 | 488 | =begin Perl 489 | 490 | use Mouse::Util::TypeConstraints; 491 | 492 | subtype 'PhoneNumber' 493 | => as 'Str' 494 | => where { m/^\d{9}$ } 495 | => message { "The string ($_) is not a valid phone number." }; 496 | 497 | =end Perl 498 | 499 | This code specifies the base data type (String), a code block that 500 | returns true for valid values, and a code block with the action or 501 | message that should be performed when the value is not valid. 502 | 503 | We can enhance our I class with some more interesting data 504 | types. For example, we know that the gender value will be one of I 505 | or I: 506 | 507 | enum 'Gender' => ('male', 'female'); 508 | 509 | C allows the user to specify how to 510 | convert between data types. For instance, if we have a C