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