1 | % $Id$ |
---|
2 | \documentclass[notes]{beamer} |
---|
3 | \usepackage[frenchb]{babel} |
---|
4 | \usepackage[T1]{fontenc} |
---|
5 | \usepackage{moreverb} |
---|
6 | \usepackage{graphicx} |
---|
7 | |
---|
8 | \mode<presentation> |
---|
9 | { |
---|
10 | \definecolor{beamerstructure}{RGB}{143,79,112} |
---|
11 | \definecolor{sidebackground}{RGB}{230,242,250} |
---|
12 | \color{beamerstructure} |
---|
13 | \usetheme{Frankfurt} |
---|
14 | \usepackage{times} |
---|
15 | \userightsidebarcolortemplate{\color{sidebackground}} |
---|
16 | \beamertemplateballitem |
---|
17 | } |
---|
18 | |
---|
19 | \title{Perl} |
---|
20 | \subtitle{5} |
---|
21 | \author{Olivier Thauvin} |
---|
22 | \date{\today} |
---|
23 | |
---|
24 | \AtBeginSection[] |
---|
25 | { |
---|
26 | \begin{frame}<beamer> |
---|
27 | \frametitle{Plan} |
---|
28 | \tableofcontents[currentsection] |
---|
29 | \end{frame} |
---|
30 | } |
---|
31 | |
---|
32 | \AtBeginSubsection[] |
---|
33 | { |
---|
34 | \begin{frame}<beamer> |
---|
35 | \frametitle{Plan} |
---|
36 | \tableofcontents[currentsubsection] |
---|
37 | \end{frame} |
---|
38 | } |
---|
39 | |
---|
40 | \begin{document} |
---|
41 | |
---|
42 | \frame{\titlepage} |
---|
43 | |
---|
44 | \section*{Sommaire} |
---|
45 | |
---|
46 | \begin{frame}{Plan} |
---|
47 | \tableofcontents |
---|
48 | \end{frame} |
---|
49 | |
---|
50 | \section{Presentation} |
---|
51 | |
---|
52 | \begin{frame} |
---|
53 | \frametitle{PERL} |
---|
54 | Perl, écrit par Larry Wall en 1987. |
---|
55 | \vfill |
---|
56 | \emph{Practical Extraction and Report Language} |
---|
57 | (\emph{langage pratique d'extraction et de génération de rapports}) |
---|
58 | \vfill |
---|
59 | \emph{Pathetically Eclectic Rubbish Lister} |
---|
60 | (\emph{collectionneur de déchets pathétiquement éclectiques}) |
---|
61 | \vfill |
---|
62 | |
---|
63 | Je parle de Perl \textbf{5} ! |
---|
64 | |
---|
65 | Dernière version 5.10.0 (Rafael Garcia-Suarez) |
---|
66 | \vfill |
---|
67 | |
---|
68 | Perl est disponible sur UNIX, Windows (et cygwin), OS2, VMS, \ldots |
---|
69 | \end{frame} |
---|
70 | |
---|
71 | \begin{frame} |
---|
72 | \frametitle{Pourquoi PERL ?} |
---|
73 | \begin{itemize} |
---|
74 | \item langage scripté, non compilé |
---|
75 | \pause |
---|
76 | \item portable |
---|
77 | \pause |
---|
78 | \item permet une écriture rapide |
---|
79 | \pause |
---|
80 | \item possède un ramasse miettes (garbage collector) |
---|
81 | \pause |
---|
82 | \item est orienté objet |
---|
83 | \pause |
---|
84 | \item possède un grand nombre de modules additionnels |
---|
85 | \end{itemize} |
---|
86 | \pause |
---|
87 | \vfill |
---|
88 | \vfill |
---|
89 | Utilisation: |
---|
90 | \begin{itemize} |
---|
91 | \item petits scripts type shell |
---|
92 | \pause |
---|
93 | \item des programmes complexes |
---|
94 | \pause |
---|
95 | \item des services de toutes sortes |
---|
96 | \pause |
---|
97 | \item des sites web |
---|
98 | \end{itemize} |
---|
99 | \end{frame} |
---|
100 | |
---|
101 | \begin{frame}[fragile] |
---|
102 | \frametitle{Exemples} |
---|
103 | Exemple de script: |
---|
104 | \vfill |
---|
105 | \begin{verbatim} |
---|
106 | #!env perl |
---|
107 | |
---|
108 | print "Hello World\n"; |
---|
109 | \end{verbatim} |
---|
110 | \vfill |
---|
111 | \vfill |
---|
112 | Sinon on peut aussi faire ça: |
---|
113 | \vfill |
---|
114 | \verb+perl -e 'print "Hello World\n"'+ |
---|
115 | \end{frame} |
---|
116 | \begin{frame}[fragile] |
---|
117 | \frametitle{bestever.pl} |
---|
118 | Sinon ça peut aussi ressembler à ça: |
---|
119 | \vfill |
---|
120 | \begin{tiny} |
---|
121 | \begin{verbatim} |
---|
122 | /;{}def/#{def}def/$_={/Times-Bold exch selectfont}#/_{rmoveto}#/"{dup}#/*/!/$ |
---|
123 | ;/q{exch}#/x ; {/J q #}#/.{/T q #}#{stringwidth}#{}#{}# 14 string dup dup dup |
---|
124 | 260 40 moveto 90 rotate ; %/}};$0='"\e[7m \e[0m"';@ARGV=split//,reverse |
---|
125 | q(ThePerl). q(Journal) x 220 ; q ; 0 T putinterval exch 7 J putinterval ; |
---|
126 | ; $_= q /m$ pop T($*!$"=!$ " )pop " * true% ? $ " $!" " !! !! % !" !" ! |
---|
127 | ! charpath {!"""}pop $ pop{""!}pop ! neg{!#}pop 220 ! neg _{!!}pop J false %T |
---|
128 | charpath clip " pop 0 " moveto 6{!!}pop $_= 105{!!}pop {$ ! $ " ! #! ##} |
---|
129 | pop{dup dup $ ! " pop pop q{"}pop 22{dup show}repeat {"}pop q 22 mul{$ "} pop |
---|
130 | neg{!#! $ "}pop ! 8 .65 mul{$ # # $}pop ! neg{"}pop _ pop{"}pop } repeat pop |
---|
131 | " { $ " ! ! ! $ " ! !" "#" #"!"""""! #" " # "m/;@ARGV=(@ARGV[-14..-1])x50;q} |
---|
132 | 0 "%};s/m[ou]|[-\dA-ln-z.\n_{}]|\$_=//gx;s/(.)(?{$*=''})/('$*.='.(++$# |
---|
133 | %2?'':"$0;").'pop;')x(ord($1)-31).'$*'/gee;s/((.(\e\[.m)*|.){77})/$1\n/g;print |
---|
134 | ; sub showpage {} |
---|
135 | \end{verbatim} |
---|
136 | \end{tiny} |
---|
137 | \vfill |
---|
138 | De Philippe Bruhat (Book). |
---|
139 | \end{frame} |
---|
140 | |
---|
141 | \section{Variables} |
---|
142 | |
---|
143 | \subsection{Variables de Perl} |
---|
144 | |
---|
145 | \begin{frame} |
---|
146 | \frametitle{types de variables} |
---|
147 | \textbf{15} types de variables: |
---|
148 | \vfill |
---|
149 | \pause |
---|
150 | \includegraphics[height=40mm]{Angel_fish} |
---|
151 | \pause |
---|
152 | \includegraphics[height=40mm]{tableau} |
---|
153 | \pause |
---|
154 | \includegraphics[height=40mm]{hache} |
---|
155 | \pause |
---|
156 | \vfill |
---|
157 | Et les autres on s'en moque... |
---|
158 | \end{frame} |
---|
159 | |
---|
160 | \subsection{Scalaire} |
---|
161 | |
---|
162 | \begin{frame}[fragile] |
---|
163 | \frametitle{scalaire} |
---|
164 | Un \textbf{scalaire} est une simple valeur: |
---|
165 | \pause |
---|
166 | \vfill |
---|
167 | \begin{itemize} |
---|
168 | \item \emph{undef} |
---|
169 | \pause |
---|
170 | \item définie: |
---|
171 | \begin{itemize} |
---|
172 | \item un chiffre, nombre |
---|
173 | \item une chaine de caractères |
---|
174 | \item une suite d'octets binaires |
---|
175 | \item \ldots |
---|
176 | \end{itemize} |
---|
177 | \end{itemize} |
---|
178 | \pause |
---|
179 | \vfill |
---|
180 | \begin{verbatim} |
---|
181 | $rien = undef; |
---|
182 | $chiffre = 42; |
---|
183 | $chaine = "1"; |
---|
184 | print $chaine + $chiffre; |
---|
185 | print $chaine . $chiffre; |
---|
186 | \end{verbatim} |
---|
187 | \pause |
---|
188 | \vfill |
---|
189 | Valeur \textit{fausses}: \textit{undef}, 0, "" |
---|
190 | |
---|
191 | Valeur \textit{vraies}: le reste: 1, "0", "un chien ?" |
---|
192 | \end{frame} |
---|
193 | |
---|
194 | \subsection{Les tableaux} |
---|
195 | |
---|
196 | \begin{frame}[fragile] |
---|
197 | \frametitle{tableau (array)} |
---|
198 | Les tableaux sont des piles de \textit{scalaires}. |
---|
199 | \pause |
---|
200 | |
---|
201 | Ils sont dynamiques, et a une seule dimension (mais ce n'est pas un problème en fait). |
---|
202 | \pause |
---|
203 | \vfill |
---|
204 | Déclarer le tableau: |
---|
205 | |
---|
206 | \verb+@tableau = ("un", 2);+ |
---|
207 | \vfill |
---|
208 | Accéder aux/Déclarer les valeurs d'un tableau (attention au \$): |
---|
209 | |
---|
210 | \begin{verbatim} |
---|
211 | $tableau[3] = 4; |
---|
212 | $tableau[-1]; # dernier élément |
---|
213 | \end{verbatim} |
---|
214 | |
---|
215 | \pause |
---|
216 | \vfill |
---|
217 | Mais en fait:\hfill |
---|
218 | \begin{tabular}{rcl} |
---|
219 | shift <=&\verb+0@@@@@n+&<= push\\ |
---|
220 | unshift =>&\verb+0@@@@@n+&=> pop\\ |
---|
221 | \end{tabular} |
---|
222 | \pause |
---|
223 | \vfill |
---|
224 | Nombre d'éléments: \verb+scalar(@tableau)+ |
---|
225 | |
---|
226 | Dernier indice: \verb+$#tableau+ |
---|
227 | \end{frame} |
---|
228 | |
---|
229 | \begin{frame}[fragile] |
---|
230 | \frametitle{hash} |
---|
231 | Ce sont des tableaux indexés \verb+clef => valeur+. |
---|
232 | |
---|
233 | Ils sont dynamiques. |
---|
234 | |
---|
235 | \pause |
---|
236 | \begin{itemize} |
---|
237 | \item la clef est une valeur textuelle simple |
---|
238 | \item la valeur est un \textbf{scalaire} |
---|
239 | \item l'ordre des clefs n'est pas conservé !!! |
---|
240 | \end{itemize} |
---|
241 | |
---|
242 | \pause |
---|
243 | Déclarer un tableau de hashage: |
---|
244 | \begin{verbatim} |
---|
245 | %hash = ( |
---|
246 | foo => "un" |
---|
247 | bar => 2 |
---|
248 | ); |
---|
249 | \end{verbatim} |
---|
250 | |
---|
251 | \pause |
---|
252 | Accéder aux/Déclarer les valeurs d'un hash (attention au \$): |
---|
253 | \begin{verbatim} |
---|
254 | $hash{baz} = 'trois'; |
---|
255 | $clef = 'clef'; |
---|
256 | $hash{$clef}; |
---|
257 | \end{verbatim} |
---|
258 | |
---|
259 | \end{frame} |
---|
260 | |
---|
261 | \subsection{Les listes} |
---|
262 | |
---|
263 | \begin{frame}[fragile] |
---|
264 | \frametitle{Comprendre les listes} |
---|
265 | \textbf{Perl aplati les listes !} |
---|
266 | |
---|
267 | Le concept est simple, mais redoutable: |
---|
268 | \pause |
---|
269 | |
---|
270 | Prenons deux tableaux de trois entrées: |
---|
271 | \begin{verbatim} |
---|
272 | @tab1 = (1, 2, 3); |
---|
273 | @tab2 = (4, 5, 6); |
---|
274 | \end{verbatim} |
---|
275 | \pause |
---|
276 | |
---|
277 | Récupérer la première entrée, et le reste de \verb+@tab1+ |
---|
278 | \begin{verbatim} |
---|
279 | ($un, @reste) = @tab1; |
---|
280 | \end{verbatim} |
---|
281 | \pause |
---|
282 | Récupérer la première entrée, et le reste de \verb+@tab1+ et \verb+@tab2+ |
---|
283 | \begin{verbatim} |
---|
284 | ($un, @reste) = (@tab1, @tab2); |
---|
285 | \end{verbatim} |
---|
286 | \pause |
---|
287 | Récupérer la deuxième entrée, et le reste de \verb+@tab1+ et \verb+@tab2+ |
---|
288 | \begin{verbatim} |
---|
289 | (undef, $deux, @reste) = (@tab1, @tab2); |
---|
290 | \end{verbatim} |
---|
291 | \end{frame} |
---|
292 | |
---|
293 | \begin{frame}[fragile] |
---|
294 | \frametitle{Comprendre les listes: les hash} |
---|
295 | Mais le pire, c'est que ce concept s'applique aussi \textbf{hash} |
---|
296 | \pause |
---|
297 | |
---|
298 | '\verb+=>+' est un séparateur comme la '\verb+,+', donc c'est une liste: |
---|
299 | \begin{verbatim} |
---|
300 | %hash = (un => 1, deux => 2); |
---|
301 | %hash = (un, 1, deux, 2); |
---|
302 | \end{verbatim} |
---|
303 | \pause |
---|
304 | Si un hash revois une liste et prends une liste\ldots |
---|
305 | \begin{verbatim} |
---|
306 | @tab = %hash; |
---|
307 | \end{verbatim} |
---|
308 | \verb+@tab+ contient "un", 1\ldots |
---|
309 | |
---|
310 | \begin{verbatim} |
---|
311 | @tab = (1, 2, 3, 4); |
---|
312 | %hash = @tab; |
---|
313 | %unhash = (%hash2, %hash3); |
---|
314 | \end{verbatim} |
---|
315 | \verb+%hash+ contient 1 \verb+=>+ 2, 3 \verb+=>+ 4\ldots |
---|
316 | |
---|
317 | Les clef dupliquées sont ecrasées, la dernière gagne ! |
---|
318 | |
---|
319 | \end{frame} |
---|
320 | |
---|
321 | \section{Syntaxe} |
---|
322 | |
---|
323 | \subsection{Syntaxe générale} |
---|
324 | |
---|
325 | \begin{frame}[fragile] |
---|
326 | \frametitle{Grandes lignes} |
---|
327 | \begin{itemize} |
---|
328 | \item les espaces sépare les mots |
---|
329 | \pause |
---|
330 | \item le '\verb+;+' sépare les instructions |
---|
331 | \pause |
---|
332 | \item le '\verb+,+' sépare les valeurs |
---|
333 | \pause |
---|
334 | \item \verb+( )+ limite une liste |
---|
335 | \pause |
---|
336 | \item \verb+{ }+ limite un bloc |
---|
337 | \pause |
---|
338 | \item tout ce qui suit un \verb+#+ est un commentaire |
---|
339 | \pause |
---|
340 | \item \verb+__END__+ mets fin au script |
---|
341 | \end{itemize} |
---|
342 | \pause |
---|
343 | |
---|
344 | Tout est permis, y compris apporter de la lisibilité à votre code: |
---|
345 | \begin{verbatim} |
---|
346 | push # création du tableau |
---|
347 | ( @tableau |
---|
348 | , $valeur, |
---|
349 | $valeur2 ); |
---|
350 | \end{verbatim} |
---|
351 | Sauf si vous tenez à faire dans l'obfu ! |
---|
352 | \end{frame} |
---|
353 | |
---|
354 | \begin{frame}[fragile] |
---|
355 | \frametitle{Structure classique d'un script} |
---|
356 | \onslide<1-6>{un shell bang:} |
---|
357 | \onslide<2-> |
---|
358 | \begin{verbatim} |
---|
359 | #!env perl |
---|
360 | \end{verbatim} |
---|
361 | \onslide<3-6>{charger les modules dont on a besoin:} |
---|
362 | \onslide<4-> |
---|
363 | \begin{verbatim} |
---|
364 | use strict; |
---|
365 | use warnings; |
---|
366 | \end{verbatim} |
---|
367 | \onslide<5-6>{arrive le code:} |
---|
368 | \onslide<6-> |
---|
369 | \begin{verbatim} |
---|
370 | while (<STDIN>) { |
---|
371 | s/ //g; |
---|
372 | print $_; |
---|
373 | } |
---|
374 | \end{verbatim} |
---|
375 | \onslide<7-> |
---|
376 | \begin{verbatim} |
---|
377 | __END__ |
---|
378 | \end{verbatim} |
---|
379 | \end{frame} |
---|
380 | |
---|
381 | \subsection{Controles d'execution} |
---|
382 | |
---|
383 | \begin{frame}[fragile] |
---|
384 | \frametitle{if} |
---|
385 | \onslide<1-> |
---|
386 | Bah... if quoi... |
---|
387 | \vfill |
---|
388 | \begin{verbatim} |
---|
389 | if ($var == 0) { |
---|
390 | print "var vaut 0\n"; |
---|
391 | } |
---|
392 | \end{verbatim} |
---|
393 | \onslide<3-> |
---|
394 | \begin{verbatim} |
---|
395 | elsif ($var == 1) { |
---|
396 | print "var vaut 1\n"; |
---|
397 | } |
---|
398 | \end{verbatim} |
---|
399 | \onslide<2-> |
---|
400 | \begin{verbatim} |
---|
401 | else { |
---|
402 | print "var vaut $var\n"; |
---|
403 | } |
---|
404 | \end{verbatim} |
---|
405 | \onslide<4-> |
---|
406 | Forme suffixée: |
---|
407 | \begin{verbatim} |
---|
408 | print "var vaut 0\n" if ($var == 0); |
---|
409 | \end{verbatim} |
---|
410 | \end{frame} |
---|
411 | |
---|
412 | \begin{frame}[fragile] |
---|
413 | \frametitle{while} |
---|
414 | Boucle tant qu'une condition est vraie: |
---|
415 | |
---|
416 | \begin{verbatim} |
---|
417 | while ($i < 100) { |
---|
418 | print "$i\n"; |
---|
419 | $i++; |
---|
420 | } |
---|
421 | \end{verbatim} |
---|
422 | \vfill |
---|
423 | \pause |
---|
424 | Forme suffixée: |
---|
425 | |
---|
426 | \begin{verbatim} |
---|
427 | $i++ while($i < 100); |
---|
428 | \end{verbatim} |
---|
429 | \end{frame} |
---|
430 | |
---|
431 | \begin{frame}[fragile] |
---|
432 | \frametitle{foreach} |
---|
433 | Boucle \textbf{pour chaque} élément d'une liste: |
---|
434 | |
---|
435 | \begin{verbatim} |
---|
436 | foreach $elem (@tableau) { |
---|
437 | print "$elem\n"; |
---|
438 | } |
---|
439 | \end{verbatim} |
---|
440 | |
---|
441 | Très pratique, cela évite de s'embeter avec les indices du tableau. |
---|
442 | \vfill |
---|
443 | \pause |
---|
444 | |
---|
445 | Forme suffixée: |
---|
446 | |
---|
447 | \begin{verbatim} |
---|
448 | print "$_\n" foreach(@tableau); |
---|
449 | \end{verbatim} |
---|
450 | \end{frame} |
---|
451 | |
---|
452 | \begin{frame}[fragile] |
---|
453 | \frametitle{Sortez moi de là} |
---|
454 | \textbf{next} fait passer à la prochaine itération d'une boucle: |
---|
455 | |
---|
456 | \begin{verbatim} |
---|
457 | foreach $val (@tableau) { |
---|
458 | next if(!$val); |
---|
459 | print "$val\n"; |
---|
460 | } |
---|
461 | \end{verbatim} |
---|
462 | \pause |
---|
463 | \vfill |
---|
464 | \textbf{last} quitte une boucle: |
---|
465 | |
---|
466 | \begin{verbatim} |
---|
467 | foreach $val (@tableau) { |
---|
468 | if ($val > 1000) { |
---|
469 | print "$val est trop haut, arret\n"; |
---|
470 | last; |
---|
471 | } |
---|
472 | print "$val\n"; |
---|
473 | } |
---|
474 | \end{verbatim} |
---|
475 | \end{frame} |
---|
476 | |
---|
477 | \subsection{Fonctions et subroutines} |
---|
478 | |
---|
479 | \begin{frame}[fragile] |
---|
480 | \frametitle{Fonctions} |
---|
481 | Déclarer une fonction: |
---|
482 | |
---|
483 | \begin{verbatim} |
---|
484 | sub fonction { |
---|
485 | print "Hello World\n"; |
---|
486 | } |
---|
487 | \end{verbatim} |
---|
488 | \pause |
---|
489 | On l'appelle: |
---|
490 | |
---|
491 | \begin{verbatim} |
---|
492 | fonction(); |
---|
493 | \end{verbatim} |
---|
494 | |
---|
495 | ou tout simplement: |
---|
496 | \begin{verbatim} |
---|
497 | fonction; |
---|
498 | \end{verbatim} |
---|
499 | \pause |
---|
500 | \vfill |
---|
501 | Sub anonyme, plus rarement utilisé: |
---|
502 | |
---|
503 | \begin{verbatim} |
---|
504 | my $sub = sub { }; |
---|
505 | $sub->(); |
---|
506 | \end{verbatim} |
---|
507 | |
---|
508 | \end{frame} |
---|
509 | |
---|
510 | \begin{frame}[fragile] |
---|
511 | \frametitle{Fonction passage d'arguments} |
---|
512 | Perl est un langage qui fonctionne avec un pile: |
---|
513 | \pause |
---|
514 | \vfill |
---|
515 | \begin{itemize} |
---|
516 | \item on pouse des élément sur la piles |
---|
517 | \item on récupère la pile |
---|
518 | \item on pose les résultat sur la pile |
---|
519 | \item on récupère la pile |
---|
520 | \end{itemize} |
---|
521 | \vfill |
---|
522 | La pile est une liste. |
---|
523 | \end{frame} |
---|
524 | |
---|
525 | \begin{frame}[fragile] |
---|
526 | \frametitle{Fonction passage d'arguments} |
---|
527 | Exemple: |
---|
528 | \pause |
---|
529 | |
---|
530 | \onslide<2-> |
---|
531 | \begin{verbatim} |
---|
532 | sub fonction { |
---|
533 | \end{verbatim} |
---|
534 | \onslide<3-> |
---|
535 | \begin{verbatim} |
---|
536 | my ($param) = @_; |
---|
537 | \end{verbatim} |
---|
538 | \onslide<4-> |
---|
539 | \begin{verbatim} |
---|
540 | return("-- $param --"); |
---|
541 | \end{verbatim} |
---|
542 | \onslide<2-> |
---|
543 | \begin{verbatim} |
---|
544 | } |
---|
545 | \end{verbatim} |
---|
546 | \onslide<2> |
---|
547 | \begin{verbatim} |
---|
548 | fonction(); |
---|
549 | \end{verbatim} |
---|
550 | \onslide<4-> |
---|
551 | \begin{verbatim} |
---|
552 | my $resultat = |
---|
553 | \end{verbatim} |
---|
554 | \onslide<3-> |
---|
555 | \begin{verbatim} |
---|
556 | fonction("perl"); |
---|
557 | \end{verbatim} |
---|
558 | |
---|
559 | \end{frame} |
---|
560 | |
---|
561 | \subsection{Déclarer ses variables} |
---|
562 | |
---|
563 | \begin{frame}[fragile] |
---|
564 | \frametitle{my and co} |
---|
565 | Perl est très laxiste: |
---|
566 | \pause |
---|
567 | |
---|
568 | \onslide<2-4> |
---|
569 | \begin{verbatim} |
---|
570 | $latitude = 5; |
---|
571 | print "$latitud\n"; |
---|
572 | \end{verbatim} |
---|
573 | |
---|
574 | \onslide<2-> |
---|
575 | Deux moyens de forcer les décalarations: |
---|
576 | \pause |
---|
577 | \begin{itemize} |
---|
578 | \item \verb+use strict;+ force la declaration des variables |
---|
579 | \pause |
---|
580 | \item \verb+use warnings;+ produit des warnings sur les actions douteuses |
---|
581 | \end{itemize} |
---|
582 | |
---|
583 | \onslide<5> |
---|
584 | \begin{verbatim} |
---|
585 | use strict; |
---|
586 | |
---|
587 | my $latitude = 5; |
---|
588 | print "$latitude\n"; |
---|
589 | \end{verbatim} |
---|
590 | |
---|
591 | |
---|
592 | \end{frame} |
---|
593 | |
---|
594 | \begin{frame}[fragile] |
---|
595 | \frametitle{Portée des variables} |
---|
596 | \verb+$var+ différent de \verb+@var+ différent de \verb+%var+ ! |
---|
597 | |
---|
598 | Sans \verb+use strict;+ Perl fait au mieux. |
---|
599 | |
---|
600 | \pause |
---|
601 | Les déclarations se font n'importe où, mais avant leur utilisation: |
---|
602 | |
---|
603 | \onslide<3-> |
---|
604 | \vfill |
---|
605 | \only<3>{Un script propre:} |
---|
606 | \only<4>{Déclaration d'une variable globale:} |
---|
607 | \only<5>{Au sein d'un bloc, on déclare un variable:} |
---|
608 | \only<6>{On peut utiliser la variable globale:} |
---|
609 | \only<7>{Attention avant l'initialisation !:} |
---|
610 | |
---|
611 | \onslide<3-> |
---|
612 | \begin{verbatim} |
---|
613 | use strict; |
---|
614 | \end{verbatim} |
---|
615 | |
---|
616 | \onslide<7-> |
---|
617 | \begin{verbatim} |
---|
618 | mafonction(); |
---|
619 | \end{verbatim} |
---|
620 | |
---|
621 | \onslide<4-> |
---|
622 | \begin{verbatim} |
---|
623 | my $var = 1 |
---|
624 | \end{verbatim} |
---|
625 | |
---|
626 | \onslide<5-> |
---|
627 | \begin{verbatim} |
---|
628 | sub mafonction { |
---|
629 | \end{verbatim} |
---|
630 | \onslide<6-> |
---|
631 | \begin{verbatim} |
---|
632 | print $var; |
---|
633 | \end{verbatim} |
---|
634 | \onslide<5-> |
---|
635 | \begin{verbatim} |
---|
636 | my $fvar = 3; |
---|
637 | } |
---|
638 | \end{verbatim} |
---|
639 | \end{frame} |
---|
640 | |
---|
641 | \begin{frame}[fragile] |
---|
642 | \frametitle{boucles et blocs} |
---|
643 | Pour les instruction qui engendre un bloc, les déclarations font partie du bloc: |
---|
644 | |
---|
645 | \pause |
---|
646 | Ici la variables \verb+$item+ apartiens au bloc \verb+foreach+: |
---|
647 | |
---|
648 | \begin{verbatim} |
---|
649 | |
---|
650 | my @tableau = (1, 2, 3, 4); |
---|
651 | |
---|
652 | foreach my $item (@tableau) { |
---|
653 | print "$item\n"; |
---|
654 | } |
---|
655 | \end{verbatim} |
---|
656 | |
---|
657 | \end{frame} |
---|
658 | |
---|
659 | |
---|
660 | \section{Variables bis} |
---|
661 | |
---|
662 | \subsection{Références} |
---|
663 | |
---|
664 | \begin{frame}[fragile] |
---|
665 | \frametitle{Référence: Concept} |
---|
666 | C'est une valeur scalaire\ldots |
---|
667 | |
---|
668 | \pause |
---|
669 | \vfill |
---|
670 | qui fait référence à un scalaire, un tableau ou un hash. |
---|
671 | |
---|
672 | \pause |
---|
673 | \vfill |
---|
674 | \begin{itemize} |
---|
675 | \item elle contient l'adresse mémoire de l'élément pointé |
---|
676 | \pause |
---|
677 | \item plusieurs références peuvent pointer la même variable |
---|
678 | \pause |
---|
679 | \item on peut faire des tableaux de références\ldots |
---|
680 | \end{itemize} |
---|
681 | \pause |
---|
682 | \vfill |
---|
683 | \textbf{Ce n'est pas un pointeur au sens C du terme}: \verb.$ref++;. ne donnera rien de bien !!!! |
---|
684 | \end{frame} |
---|
685 | |
---|
686 | \begin{frame}[fragile] |
---|
687 | \frametitle{Créer des références} |
---|
688 | \pause |
---|
689 | Référence de tableaux: |
---|
690 | \pause |
---|
691 | \vfill |
---|
692 | \begin{verbatim} |
---|
693 | my @tableau = (1, 2, 3); |
---|
694 | \end{verbatim} |
---|
695 | \pause |
---|
696 | Récupérer une référence vers: |
---|
697 | \begin{verbatim} |
---|
698 | my $tabref = \@tableau; |
---|
699 | \end{verbatim} |
---|
700 | \pause |
---|
701 | Créer une référence vers un nouveau tableau: |
---|
702 | \begin{verbatim} |
---|
703 | my $tabref = [ @tableau ]; |
---|
704 | \end{verbatim} |
---|
705 | \vfill |
---|
706 | \vfill |
---|
707 | \pause |
---|
708 | Référence de hash: |
---|
709 | \pause |
---|
710 | \vfill |
---|
711 | \begin{verbatim} |
---|
712 | my %hash = (a => 1); |
---|
713 | \end{verbatim} |
---|
714 | \pause |
---|
715 | Récupérer une référence vers: |
---|
716 | \begin{verbatim} |
---|
717 | my $hashref = \%hash; |
---|
718 | \end{verbatim} |
---|
719 | \pause |
---|
720 | Créer une référence vers un nouveau tableau: |
---|
721 | \begin{verbatim} |
---|
722 | my $hashref = { %hash } ; |
---|
723 | \end{verbatim} |
---|
724 | \end{frame} |
---|
725 | |
---|
726 | \begin{frame}[fragile] |
---|
727 | \frametitle{Utiliser les références} |
---|
728 | Pour accéder au contenu la variable référencée on utilise \verb+->+. |
---|
729 | \vfill |
---|
730 | Pour acceder à la variable elle même: \verb+$$+, \verb+@$+ et \verb+%$+ pour |
---|
731 | respectivement les scalaires, tableaux et les hash. |
---|
732 | |
---|
733 | \pause |
---|
734 | On créer un réference: |
---|
735 | \begin{verbatim} |
---|
736 | my $tabref = [ 1, 2, 3 ]; |
---|
737 | my $hashref = { a => 1 }; |
---|
738 | \end{verbatim} |
---|
739 | \pause |
---|
740 | On accède au tableau via la référence: |
---|
741 | \begin{verbatim} |
---|
742 | $tabref->[0]; |
---|
743 | $hashref->{a}; |
---|
744 | \end{verbatim} |
---|
745 | \pause |
---|
746 | Après tout est presque pareil: |
---|
747 | \begin{verbatim} |
---|
748 | scalar(@$tabref); |
---|
749 | keys %$hashref; |
---|
750 | \end{verbatim} |
---|
751 | |
---|
752 | \end{frame} |
---|
753 | |
---|
754 | \begin{frame}[fragile] |
---|
755 | \frametitle{Pourquoi les références ?} |
---|
756 | Perl aplati les listes, remember ? |
---|
757 | |
---|
758 | \pause |
---|
759 | \begin{verbatim} |
---|
760 | my @tab1 = (1,2); my @tab2 = (3,4); |
---|
761 | my @tab = (@tab1, @tab2); |
---|
762 | \end{verbatim} |
---|
763 | \verb+@tab+ contient 4 éléments... |
---|
764 | \pause |
---|
765 | \begin{verbatim} |
---|
766 | my @tab1 = (1,2); my @tab2 = (3,4); |
---|
767 | my @tab = (\@tab1, \@tab2); |
---|
768 | \end{verbatim} |
---|
769 | \verb+@tab+ contient 2 éléments qui réferencent des tableaux. |
---|
770 | \pause |
---|
771 | |
---|
772 | On accède aux valeurs: |
---|
773 | \begin{verbatim} |
---|
774 | $tab[0]->[0]; |
---|
775 | \end{verbatim} |
---|
776 | \pause |
---|
777 | Ou même plus simplement: |
---|
778 | \begin{verbatim} |
---|
779 | $tab[0][0]; |
---|
780 | \end{verbatim} |
---|
781 | \ldots qui revient à faire un tableau à 2 dimensions. |
---|
782 | |
---|
783 | \end{frame} |
---|
784 | |
---|
785 | \begin{frame}[fragile] |
---|
786 | \frametitle{Références: copain de la mémoire} |
---|
787 | \begin{small} |
---|
788 | \begin{verbatim} |
---|
789 | # très gros tableau |
---|
790 | my @tab = (); |
---|
791 | |
---|
792 | fonc(@tab); |
---|
793 | |
---|
794 | sub fonc { |
---|
795 | my @tab = @_; # Créer un copie complète |
---|
796 | } |
---|
797 | \end{verbatim} |
---|
798 | \pause |
---|
799 | \begin{verbatim} |
---|
800 | # très gros tableau |
---|
801 | my @tab = (); |
---|
802 | |
---|
803 | fonc(\@tab); |
---|
804 | |
---|
805 | sub fonc { |
---|
806 | my ($tab) = @_; # Ne copie que la référence |
---|
807 | } |
---|
808 | \end{verbatim} |
---|
809 | \end{small} |
---|
810 | \end{frame} |
---|
811 | |
---|
812 | \begin{frame}[fragile] |
---|
813 | \frametitle{Références: les pièges} |
---|
814 | Déférencer un undef: |
---|
815 | \begin{verbatim} |
---|
816 | perl -we 'my $ref; print @{ $ref }' |
---|
817 | Use of uninitialized value $ref in |
---|
818 | array dereference at -e line 1. |
---|
819 | \end{verbatim} |
---|
820 | \pause |
---|
821 | Voici l'astuce: |
---|
822 | \begin{verbatim} |
---|
823 | perl -we 'my $ref; print @{ $ref || [] }' |
---|
824 | \end{verbatim} |
---|
825 | \pause |
---|
826 | \vfill |
---|
827 | \vfill |
---|
828 | Attention au type de variable référencée: |
---|
829 | \begin{verbatim} |
---|
830 | perl -we 'my $ref = {}; print @{ $ref }' |
---|
831 | Not an ARRAY reference at -e line 1. |
---|
832 | \end{verbatim} |
---|
833 | \pause |
---|
834 | \vfill |
---|
835 | \vfill |
---|
836 | Attention au structure complexes: |
---|
837 | \begin{verbatim} |
---|
838 | $ref->[0][2]{daleks}[0]{episode} |
---|
839 | {$ennemi}{histoire}[0]; |
---|
840 | \end{verbatim} |
---|
841 | \end{frame} |
---|
842 | |
---|
843 | \begin{frame}[fragile] |
---|
844 | \frametitle{Références: dernières choses} |
---|
845 | On peut connaitre le type de référence avec \verb+ref()+ |
---|
846 | \begin{verbatim} |
---|
847 | perl -we 'my $ref = {}; print ref $ref' |
---|
848 | HASH |
---|
849 | \end{verbatim} |
---|
850 | \vfill |
---|
851 | \vfill |
---|
852 | \pause |
---|
853 | Tout est là:\\ |
---|
854 | \verb+man perlreref+ |
---|
855 | \end{frame} |
---|
856 | |
---|
857 | \subsection{les descripteurs de fichier} |
---|
858 | |
---|
859 | \begin{frame}[fragile] |
---|
860 | \frametitle{Fichiers: Version simple} |
---|
861 | Version moderne: |
---|
862 | |
---|
863 | Ouvrir un fichier en lecture: |
---|
864 | \begin{verbatim} |
---|
865 | open(my $handle, '<', 'fichier'); |
---|
866 | \end{verbatim} |
---|
867 | \pause |
---|
868 | Lire une ligne: |
---|
869 | \begin{verbatim} |
---|
870 | my $line = <$handle>; |
---|
871 | chomp($line); # strip cariage return |
---|
872 | \end{verbatim} |
---|
873 | \pause |
---|
874 | Fermer le fichier: |
---|
875 | \begin{verbatim} |
---|
876 | close($handle); |
---|
877 | \end{verbatim} |
---|
878 | \pause |
---|
879 | \vfill |
---|
880 | Ancienne méthode: |
---|
881 | \begin{verbatim} |
---|
882 | open(HANDLE, '<', 'fichier'); |
---|
883 | my $line = <HANDLE>; |
---|
884 | chomp($line); # strip cariage return |
---|
885 | close(HANDLE); |
---|
886 | \end{verbatim} |
---|
887 | \end{frame} |
---|
888 | |
---|
889 | \begin{frame}[fragile] |
---|
890 | \frametitle{Fichier: version simple (suite)} |
---|
891 | |
---|
892 | Ouvrir: |
---|
893 | \begin{verbatim} |
---|
894 | open(my $handle, '>', 'fichier'); |
---|
895 | \end{verbatim} |
---|
896 | \pause |
---|
897 | Écrire: |
---|
898 | \begin{verbatim} |
---|
899 | print $handle "Exterminate !\n"; |
---|
900 | \end{verbatim} |
---|
901 | \begin{textbf}Attention à \verb+print+: pas de \verb+,+ après le file handle\end{textbf} |
---|
902 | |
---|
903 | \pause |
---|
904 | \vfill |
---|
905 | Fermer: |
---|
906 | \begin{verbatim} |
---|
907 | close($handle); |
---|
908 | \end{verbatim} |
---|
909 | \pause |
---|
910 | Trois file handle par défaut: \verb+STDIN+, \verb+STDOUT+, \verb+STDERR+ |
---|
911 | (entrée, sortie, sortie d'erreur). |
---|
912 | \end{frame} |
---|
913 | |
---|
914 | \begin{frame}[fragile] |
---|
915 | \frametitle{Fichiers: Version compliquée} |
---|
916 | Pour des besoins très spécifiques: |
---|
917 | \begin{itemize} |
---|
918 | \item open()/close() |
---|
919 | \item read()/print() |
---|
920 | \end{itemize} |
---|
921 | |
---|
922 | \vfill |
---|
923 | ou: |
---|
924 | \vfill |
---|
925 | \pause |
---|
926 | \begin{itemize} |
---|
927 | \item sysopen()/sysclose() |
---|
928 | \item sysread()/syswrite() |
---|
929 | \end{itemize} |
---|
930 | \pause |
---|
931 | \vfill |
---|
932 | \vfill |
---|
933 | Si vraiment vous voulez en savoir plus: \verb+man+ |
---|
934 | \begin{itemize} |
---|
935 | \item perlfunc |
---|
936 | \pause |
---|
937 | \item perlopentut |
---|
938 | \end{itemize} |
---|
939 | \end{frame} |
---|
940 | |
---|
941 | \section{RegExp (Expressions régulières)} |
---|
942 | |
---|
943 | \begin{frame} |
---|
944 | \frametitle{Perl ça pue...} |
---|
945 | \vfill |
---|
946 | \emph{Perl ça pue, y'a pas de fonction pour travailler avec des regexp comme en php} |
---|
947 | \vfill |
---|
948 | \pause |
---|
949 | |
---|
950 | Les expressions régulières de Perl sont très puissantes ! |
---|
951 | |
---|
952 | \pause |
---|
953 | \vfill |
---|
954 | Ce qui a donné la lib PCRE (Perl Compatible Regular Expressions). |
---|
955 | \end{frame} |
---|
956 | |
---|
957 | \section{Cookbook} |
---|
958 | |
---|
959 | \section*{Fin} |
---|
960 | |
---|
961 | \begin{frame}{Questions ?} |
---|
962 | \center{ |
---|
963 | \hfill |
---|
964 | } |
---|
965 | \end{frame} |
---|
966 | |
---|
967 | \end{document} |
---|