1 | % $Id$ |
---|
2 | |
---|
3 | \section{Syntaxe} |
---|
4 | |
---|
5 | \subsection{Syntaxe générale} |
---|
6 | |
---|
7 | \begin{frame}[fragile] |
---|
8 | \frametitle{Grandes lignes} |
---|
9 | \begin{itemize} |
---|
10 | \item les espaces sépare les mots |
---|
11 | \pause |
---|
12 | \item le '\verb+;+' sépare les instructions |
---|
13 | \pause |
---|
14 | \item le '\verb+,+' sépare les valeurs |
---|
15 | \pause |
---|
16 | \item \verb+( )+ limite une liste |
---|
17 | \pause |
---|
18 | \item \verb+{ }+ limite un bloc |
---|
19 | \pause |
---|
20 | \item tout ce qui suit un \verb+#+ est un commentaire |
---|
21 | \pause |
---|
22 | \item \verb+__END__+ indique la fin des instructions |
---|
23 | \end{itemize} |
---|
24 | \pause |
---|
25 | |
---|
26 | \begin{exampleblock}{Tout est permis, y compris apporter de la lisibilité à votre code:} |
---|
27 | \begin{verbatim} |
---|
28 | push # création du tableau |
---|
29 | ( @tableau |
---|
30 | , $valeur, $valeur2 ); |
---|
31 | \end{verbatim} |
---|
32 | \end{exampleblock} |
---|
33 | \end{frame} |
---|
34 | |
---|
35 | \begin{frame}[fragile] |
---|
36 | \frametitle{Structure classique d'un script} |
---|
37 | \onslide<1-6>{un shell bang:} |
---|
38 | \onslide<2-> |
---|
39 | \begin{verbatim} |
---|
40 | #!env perl |
---|
41 | \end{verbatim} |
---|
42 | \onslide<3-6>{charger les modules dont on a besoin:} |
---|
43 | \onslide<4-> |
---|
44 | \begin{verbatim} |
---|
45 | use strict; |
---|
46 | use warnings; |
---|
47 | \end{verbatim} |
---|
48 | \onslide<5-6>{arrive le code:} |
---|
49 | \onslide<6-> |
---|
50 | \begin{verbatim} |
---|
51 | while (<STDIN>) { |
---|
52 | s/ //g; |
---|
53 | print $_; |
---|
54 | } |
---|
55 | \end{verbatim} |
---|
56 | \onslide<7-> |
---|
57 | \begin{verbatim} |
---|
58 | __END__ |
---|
59 | \end{verbatim} |
---|
60 | \end{frame} |
---|
61 | |
---|
62 | \subsection{Opérateurs} |
---|
63 | |
---|
64 | \begin{frame}[fragile] |
---|
65 | \frametitle{Opérateur} |
---|
66 | \vfill |
---|
67 | \begin{block}{Une courte liste} |
---|
68 | \begin{tabular}{ll} |
---|
69 | \verb;+ - * /;&devinez !\\ |
---|
70 | \verb+%+&modulo\\ |
---|
71 | \pause |
---|
72 | \verb;&& ||;&et/ou logique\\ |
---|
73 | \verb;and or;&et/ou\\ |
---|
74 | \verb+? :+&if then else\\ |
---|
75 | \pause |
---|
76 | \verb;.;&concaténation de chaine\\ |
---|
77 | \pause |
---|
78 | \verb;& |;&et/ou logique (bit)\\ |
---|
79 | \verb;<< >>;&décalage de bit\\ |
---|
80 | \end{tabular} |
---|
81 | \end{block} |
---|
82 | \vfill |
---|
83 | \begin{block}{} |
---|
84 | \verb+man perlop+ |
---|
85 | \end{block} |
---|
86 | \vfill |
---|
87 | \end{frame} |
---|
88 | |
---|
89 | \subsection{Controles d'execution} |
---|
90 | |
---|
91 | \begin{frame}[fragile] |
---|
92 | \frametitle{if} |
---|
93 | \begin{exampleblock}{Bah... if quoi...} |
---|
94 | \onslide<1-> |
---|
95 | \vfill |
---|
96 | \begin{verbatim} |
---|
97 | if ($var == 0) { |
---|
98 | print "var vaut 0\n"; |
---|
99 | } |
---|
100 | \end{verbatim} |
---|
101 | \onslide<3-> |
---|
102 | \begin{verbatim} |
---|
103 | elsif ($var == 1) { |
---|
104 | print "var vaut 1\n"; |
---|
105 | } |
---|
106 | \end{verbatim} |
---|
107 | \onslide<2-> |
---|
108 | \begin{verbatim} |
---|
109 | else { |
---|
110 | print "var vaut $var\n"; |
---|
111 | } |
---|
112 | \end{verbatim} |
---|
113 | \end{exampleblock} |
---|
114 | \onslide<4-> |
---|
115 | \begin{block}{Forme suffixée:} |
---|
116 | \begin{verbatim} |
---|
117 | print "var vaut 0\n" if ($var == 0); |
---|
118 | \end{verbatim} |
---|
119 | \end{block} |
---|
120 | \end{frame} |
---|
121 | |
---|
122 | \begin{frame}[fragile] |
---|
123 | \frametitle{while} |
---|
124 | \begin{exampleblock}{Boucle tant qu'une condition est vraie:} |
---|
125 | \begin{verbatim} |
---|
126 | while ($i < 100) { |
---|
127 | print "$i\n"; |
---|
128 | $i++; |
---|
129 | } |
---|
130 | \end{verbatim} |
---|
131 | \pause |
---|
132 | \begin{verbatim} |
---|
133 | do { |
---|
134 | print "$i\n"; |
---|
135 | $i++; |
---|
136 | } while($i < 100); |
---|
137 | \end{verbatim} |
---|
138 | \end{exampleblock} |
---|
139 | \vfill |
---|
140 | \pause |
---|
141 | \begin{block}{Forme suffixée:} |
---|
142 | \begin{verbatim} |
---|
143 | $i++ while($i < 100); |
---|
144 | \end{verbatim} |
---|
145 | \end{block} |
---|
146 | \end{frame} |
---|
147 | |
---|
148 | \begin{frame}[fragile] |
---|
149 | \frametitle{foreach} |
---|
150 | \begin{exampleblock}{Boucle \textbf{pour chaque} élément d'une liste:} |
---|
151 | \begin{verbatim} |
---|
152 | foreach $elem (@tableau) { |
---|
153 | print "$elem\n"; |
---|
154 | } |
---|
155 | \end{verbatim} |
---|
156 | \end{exampleblock} |
---|
157 | |
---|
158 | Très pratique, cela évite de s'embeter avec les indices du tableau. |
---|
159 | \vfill |
---|
160 | \pause |
---|
161 | |
---|
162 | \begin{block}{Forme suffixée:} |
---|
163 | \begin{verbatim} |
---|
164 | print "$_\n" foreach(@tableau); |
---|
165 | \end{verbatim} |
---|
166 | \end{block} |
---|
167 | \end{frame} |
---|
168 | |
---|
169 | \begin{frame}[fragile] |
---|
170 | \frametitle{Sortez moi de là} |
---|
171 | \begin{block}{\textbf{next} fait passer à la prochaine itération d'une boucle:} |
---|
172 | \begin{verbatim} |
---|
173 | foreach $val (@tableau) { |
---|
174 | next if(!$val); |
---|
175 | print "$val\n"; |
---|
176 | } |
---|
177 | \end{verbatim} |
---|
178 | \end{block} |
---|
179 | \pause |
---|
180 | \vfill |
---|
181 | \begin{block}{\textbf{last} quitte une boucle:} |
---|
182 | \begin{verbatim} |
---|
183 | foreach $val (@tableau) { |
---|
184 | if ($val > 1000) { |
---|
185 | print "$val est trop haut, arret\n"; |
---|
186 | last; |
---|
187 | } |
---|
188 | print "$val\n"; |
---|
189 | } |
---|
190 | \end{verbatim} |
---|
191 | \end{block} |
---|
192 | \end{frame} |
---|
193 | |
---|
194 | |
---|
195 | \subsection{Fonctions et subroutines} |
---|
196 | |
---|
197 | \begin{frame}[fragile] |
---|
198 | \frametitle{Fonctions} |
---|
199 | \begin{block}{Déclarer une fonction:} |
---|
200 | \begin{verbatim} |
---|
201 | sub fonction { |
---|
202 | print "Hello World\n"; |
---|
203 | } |
---|
204 | \end{verbatim} |
---|
205 | \end{block} |
---|
206 | \pause |
---|
207 | \begin{block}{Utilisation} |
---|
208 | \begin{verbatim} |
---|
209 | fonction(); |
---|
210 | |
---|
211 | fonction; |
---|
212 | \end{verbatim} |
---|
213 | \end{block} |
---|
214 | \pause |
---|
215 | \vfill |
---|
216 | \begin{block}{\textit{Sub anonyme}, plus rarement utilisé:} |
---|
217 | \begin{verbatim} |
---|
218 | my $sub = sub { }; |
---|
219 | $sub->(); |
---|
220 | \end{verbatim} |
---|
221 | \end{block} |
---|
222 | \end{frame} |
---|
223 | |
---|
224 | \begin{frame}[fragile] |
---|
225 | \frametitle{Fonction: passage d'arguments} |
---|
226 | \begin{block}{les arguments sont passé dans \textbf{@\_}} |
---|
227 | \only<2>{On passe 1 arguments} |
---|
228 | \only<3>{On n arguments (ici 2)} |
---|
229 | \only<5>{on rends le premier arguments obligatoire, les autres |
---|
230 | sont optionnels} |
---|
231 | \onslide<4>{Via un hash: |
---|
232 | \begin{itemize} |
---|
233 | \item arguments nommés |
---|
234 | \item arguments optionnels |
---|
235 | \end{itemize}} |
---|
236 | \end{block} |
---|
237 | \begin{exampleblock}{} |
---|
238 | \pause |
---|
239 | \begin{semiverbatim} |
---|
240 | sub affiche \{ |
---|
241 | (\only<2-3,5->{$arg}\only<3,5->{, }\only<3>{$arg2}\only<4->{\%hash}) = @_; |
---|
242 | print \only<2-3,5->{"$arg\\n"}\only<4>{$hash\{key1\}}; |
---|
243 | \} |
---|
244 | |
---|
245 | affiche(\only<2,3,5->{"foo"}\only<3,5->{, }\only<3>{"bar"}\only<4->{key1 => "val1"}); |
---|
246 | \end{semiverbatim} |
---|
247 | \end{exampleblock} |
---|
248 | \end{frame} |
---|
249 | |
---|
250 | \begin{frame}[fragile] |
---|
251 | \frametitle{Retours des valeurs} |
---|
252 | \begin{block}{la dernière valeur est retournée} |
---|
253 | \pause |
---|
254 | \only<2-3>{On renvoit \only<2>{explicitement}\only<3>{implicitement} \$res |
---|
255 | \only<3>{ (dernière valeur)}} |
---|
256 | \only<4>{On renvoit explicitement le résultat} |
---|
257 | \only<5>{le resultat est la dernière valeur\ldots} |
---|
258 | \end{block} |
---|
259 | \begin{exampleblock}{} |
---|
260 | \begin{semiverbatim} |
---|
261 | sub addition \{ |
---|
262 | ($un, $deux) = @\_; |
---|
263 | \only<2-3>{$res = }\only<4>{return }$un + $deux; |
---|
264 | \only<2>{return }\only<2-3>{$res} |
---|
265 | \} |
---|
266 | |
---|
267 | print addition(2, 4); |
---|
268 | \end{semiverbatim} |
---|
269 | \end{exampleblock} |
---|
270 | \end{frame} |
---|
271 | |
---|
272 | \subsection{Traitement sur les listes} |
---|
273 | |
---|
274 | \begin{frame}[fragile] |
---|
275 | \frametitle{sort} |
---|
276 | |
---|
277 | \begin{block}{Fonction de tri} |
---|
278 | Evalue la comparaison entre \verb+$a+ et \verb+$b+ |
---|
279 | |
---|
280 | Selon le résultat, l'une ou l'autre passe devant |
---|
281 | \end{block} |
---|
282 | \pause |
---|
283 | |
---|
284 | \vfill |
---|
285 | \begin{exampleblock}{} |
---|
286 | \begin{verbatim} |
---|
287 | my @tableau = ('010', '1', '12'); |
---|
288 | \end{verbatim} |
---|
289 | \end{exampleblock} |
---|
290 | \pause |
---|
291 | |
---|
292 | \vfill |
---|
293 | \begin{exampleblock}{tri numérique} |
---|
294 | \begin{verbatim} |
---|
295 | sort { $a <=> $b } @tableau; |
---|
296 | \end{verbatim} |
---|
297 | \end{exampleblock} |
---|
298 | \pause |
---|
299 | |
---|
300 | \vfill |
---|
301 | \begin{exampleblock}{tri alphabétique} |
---|
302 | \begin{verbatim} |
---|
303 | sort { $a cmp $b } @tableau; |
---|
304 | \end{verbatim} |
---|
305 | \end{exampleblock} |
---|
306 | \pause |
---|
307 | |
---|
308 | \vfill |
---|
309 | \begin{exampleblock}{tri numérique inverse} |
---|
310 | \begin{verbatim} |
---|
311 | sort { $b <=> $a } @tableau; |
---|
312 | \end{verbatim} |
---|
313 | \end{exampleblock} |
---|
314 | |
---|
315 | \end{frame} |
---|
316 | |
---|
317 | \begin{frame}[fragile] |
---|
318 | \frametitle{grep} |
---|
319 | \begin{block}{Fonction de filtre} |
---|
320 | Revois chaque valeur placée dans \verb+$_+ si la condition renvoyée est vraie |
---|
321 | \end{block} |
---|
322 | \pause |
---|
323 | |
---|
324 | \vfill |
---|
325 | \begin{exampleblock}{} |
---|
326 | |
---|
327 | |
---|
328 | \begin{verbatim} |
---|
329 | my @tableau = ('010', '1', '12'); |
---|
330 | \end{verbatim} |
---|
331 | \end{exampleblock} |
---|
332 | \pause |
---|
333 | |
---|
334 | \vfill |
---|
335 | \begin{exampleblock}{valeur vraie} |
---|
336 | \begin{verbatim} |
---|
337 | grep { $_ } @tableau; |
---|
338 | \end{verbatim} |
---|
339 | \end{exampleblock} |
---|
340 | \pause |
---|
341 | |
---|
342 | \vfill |
---|
343 | \begin{exampleblock}{avec une condition} |
---|
344 | \begin{verbatim} |
---|
345 | grep { $_ > 10 } @tableau; |
---|
346 | \end{verbatim} |
---|
347 | \end{exampleblock} |
---|
348 | \pause |
---|
349 | |
---|
350 | \vfill |
---|
351 | \begin{exampleblock}{sans utiliser \$\_} |
---|
352 | \begin{verbatim} |
---|
353 | my $vrai = 1 |
---|
354 | grep { $vrai } @tableau; |
---|
355 | \end{verbatim} |
---|
356 | \end{exampleblock} |
---|
357 | \end{frame} |
---|
358 | |
---|
359 | \begin{frame}[fragile] |
---|
360 | \frametitle{map} |
---|
361 | \begin{block}{mappage des valeurs} |
---|
362 | Remplace chaque valeur par l'évaluation retournée |
---|
363 | |
---|
364 | Chaque valeur est placé dans \verb+$_+ |
---|
365 | \end{block} |
---|
366 | \pause |
---|
367 | |
---|
368 | \vfill |
---|
369 | \begin{exampleblock}{} |
---|
370 | \begin{verbatim} |
---|
371 | my @tableau = ('010', '1', '12'); |
---|
372 | \end{verbatim} |
---|
373 | \end{exampleblock} |
---|
374 | \pause |
---|
375 | |
---|
376 | \vfill |
---|
377 | \begin{exampleblock}{renvois chaque valeur + 1} |
---|
378 | \begin{verbatim} |
---|
379 | map { $_ + 1 } @tableau; |
---|
380 | \end{verbatim} |
---|
381 | \end{exampleblock} |
---|
382 | \pause |
---|
383 | |
---|
384 | \vfill |
---|
385 | \begin{exampleblock}{renvois chaque valeur et chaque valeur + 1} |
---|
386 | \begin{verbatim} |
---|
387 | map { $_, $_ + 1 } @tableau; |
---|
388 | \end{verbatim} |
---|
389 | \end{exampleblock} |
---|
390 | \end{frame} |
---|
391 | |
---|
392 | \begin{frame}[fragile] |
---|
393 | \frametitle{Combinaison pour la forme} |
---|
394 | |
---|
395 | \begin{exampleblock}{humpff\ldots} |
---|
396 | \begin{semiverbatim} |
---|
397 | my @communes = |
---|
398 | \onslide<5->{# première lettre en majuscule} |
---|
399 | map \{ ucfirst($\_) \} |
---|
400 | \onslide<4->{# tri: ordre alphabétique} |
---|
401 | sort \{ $a cmp $b \} |
---|
402 | \onslide<3->{# filtre: première lettre est un m} |
---|
403 | grep \{ substr($\_, 0, 1) eq 'm' \} |
---|
404 | \onslide<2->{# une liste de villes} |
---|
405 | ('nantes', |
---|
406 | 'mont de marsan', 'amiens', 'nancy', 'dijon', |
---|
407 | 'bordeaux', 'metz', bordeaux', 'bordeaux', ); |
---|
408 | \end{semiverbatim} |
---|
409 | \end{exampleblock} |
---|
410 | \end{frame} |
---|
411 | |
---|
412 | \subsection{Déclarer ses variables} |
---|
413 | |
---|
414 | \begin{frame}[fragile] |
---|
415 | \frametitle{my and compagny} |
---|
416 | \begin{block}{Perl est très laxiste:} |
---|
417 | \pause |
---|
418 | \only<2>{Voici un code qui fonctionne, mais pas comme on veut\ldots} |
---|
419 | \onslide<3->{ |
---|
420 | Deux moyens de forcer les décalarations: |
---|
421 | \pause |
---|
422 | \begin{itemize} |
---|
423 | \item \textbf{use strict;} force la declaration des variables |
---|
424 | \pause |
---|
425 | \item \textbf{use warnings;} produit des warnings sur les actions douteuses |
---|
426 | \end{itemize} |
---|
427 | } |
---|
428 | \only<6->{\textbf{my} sert à déclarer une variable\\ |
---|
429 | } |
---|
430 | \pause |
---|
431 | \only<8->{Voilà qui est parfait !} |
---|
432 | \end{block} |
---|
433 | |
---|
434 | \onslide<5,7>{ |
---|
435 | \begin{alertblock}{l'erreur:} |
---|
436 | \only<5>{Global symbol "\$latitude" requires explicit package} |
---|
437 | \only<7>{Global symbol "\$latitud" requires explicit package} |
---|
438 | \end{alertblock} |
---|
439 | } |
---|
440 | |
---|
441 | \onslide<1-> |
---|
442 | \begin{exampleblock}{} |
---|
443 | \begin{semiverbatim} |
---|
444 | \onslide<5->{use strict; |
---|
445 | } |
---|
446 | \only<6->{my }$latitude = 5; |
---|
447 | print "$latitud\only<8->{e}\\n"; |
---|
448 | \end{semiverbatim} |
---|
449 | \end{exampleblock} |
---|
450 | |
---|
451 | \end{frame} |
---|
452 | |
---|
453 | \begin{frame} |
---|
454 | \frametitle{The use strict rules} |
---|
455 | |
---|
456 | \begin{alertblock}{J'ai bien pris note que:} |
---|
457 | \only<2->{ |
---|
458 | J'utiliserais au moins \textit{use strict}\\ |
---|
459 | } |
---|
460 | \only<3->{ |
---|
461 | J'utiliserais \textbf{toujours} au moins \textit{use strict}\\ |
---|
462 | } |
---|
463 | \only<4->{ |
---|
464 | \textbf{Jamais} je ne dérangerais un gourou Perl si je n'ai pas utilisé |
---|
465 | \textit{use strict}\\ |
---|
466 | } |
---|
467 | |
---|
468 | \end{alertblock} |
---|
469 | \end{frame} |
---|
470 | |
---|
471 | \begin{frame}[fragile] |
---|
472 | \frametitle{Portée des variables} |
---|
473 | \begin{alertblock}{} |
---|
474 | \verb+$var+ différent de \verb+@var+ différent de \verb+%var+ ! |
---|
475 | \end{alertblock} |
---|
476 | \pause |
---|
477 | \begin{block}{les règles} |
---|
478 | Sans \verb+use strict;+ ni \verb+my+ Perl fait au mieux.\\ |
---|
479 | \pause |
---|
480 | Les déclarations se font n'importe où, mais avant leur utilisation. |
---|
481 | \end{block} |
---|
482 | \pause |
---|
483 | \begin{exampleblock}{example:} |
---|
484 | \onslide<4-> |
---|
485 | \begin{semiverbatim} |
---|
486 | use strict; |
---|
487 | \onslide<5->{mafonction();} |
---|
488 | my $var = 1 |
---|
489 | sub mafonction \{ |
---|
490 | print $var; |
---|
491 | my $fvar = 3; |
---|
492 | \} |
---|
493 | \end{semiverbatim} |
---|
494 | \end{exampleblock} |
---|
495 | \end{frame} |
---|
496 | |
---|
497 | \begin{frame}[fragile] |
---|
498 | \frametitle{boucles et blocs} |
---|
499 | \begin{exampleblock}{limiter la portée en utilisant un bloc} |
---|
500 | \begin{verbatim} |
---|
501 | my $globalvar = 1; |
---|
502 | { |
---|
503 | my $blockvar = 2; |
---|
504 | } |
---|
505 | \end{verbatim} |
---|
506 | \end{exampleblock} |
---|
507 | \pause |
---|
508 | |
---|
509 | \begin{exampleblock}{Déclaration dans les boucles} |
---|
510 | \begin{verbatim} |
---|
511 | |
---|
512 | my @tableau = (1, 2, 3, 4); |
---|
513 | |
---|
514 | foreach my $item (@tableau) { |
---|
515 | print "$item\n"; |
---|
516 | } |
---|
517 | \end{verbatim} |
---|
518 | \end{exampleblock} |
---|
519 | \end{frame} |
---|
520 | |
---|
521 | |
---|