Tutorial This tutorial will take you headlong into programming Perl. Make sure that you have a recent perl installed (5.10 or newer) and that you were able to complete the "Hello World" example in Getting Started. Ce tutoriel vous plongera la tête la première dans la programmation Perl. Assurez-vous que vous avez récemment installé un programme Perl (5.10 ou plus récent) et que vous étes en mesure de comprendre l'exemple "Bonjour le monde" dans Getting Started. As you work through this tutorial, take the time to read the perldoc links (you can also use the perldoc program to lookup documentation from your console.) Several concepts are being introduced and may take some effort to grasp. Some readers may prefer to plow through the tutorial and then go back through to make sense of the details. Pendant que vous parcourrez ce tutorial, prenez le temps de lire les liens perldoc (vous pouvez aussi utiliser le programme perldoc pour rechercher la documentation de votre console.) Plusieurs concepts ont été introduits et peuvent demander un certain effort pour être comprit. Certains lecteurs peuvent préfèrer tracer dans le tutoriel, puis y revenir afin de comprendre les détails. You should actually type the examples rather than using copy+paste. This will help you to learn and remember the feel of the Perl syntax. If you make a typo, perl will probably throw an error or warning — learning to understand perl's error messages and use them to find your typos is part of the process. Vous devriez effectivement taper (entrer à la console) les exemples plutôt que d'utiliser le "copier + coller". Cela vous aidera à apprendre et à mémoriser les tournures de la syntaxe Perl. Si vous commettez une faute de frappe, Perl va probablement décocher une erreur ou un avertissement - apprendre à comprendre les messages d'erreur de Perl et de les utiliser pour trouver vos fautes de frappe fait partie du processus. Solving a Problem - Résoudre un problème Let's say that your office has decided to keep track of how many bugs each developer fixes and each team is going to award prizes to the developer with the most bugs fixed per week. On your first day at work, your manager Sam gives you this input file and asks you to write a program to determine who wins the USB rocket launcher on your team (team 42). Disons que votre bureau a décidé de garder une trace de combien de bogues chaque développeur fixe (réppare) et chaque équipe va décerner des prix pour le développeur avec le plus de bugs fixés par semaine. Sur votre premier jour de travail, votre manager Sam vous donne ce fichier d'entrée et vous demande d'écrire un programme pour déterminer qui gagne le USB rocket launcher de votre équipe (équipe 42). Team 7 John: 19 Sue: 20 Pam: 35 Team 42 Jeff: 12 Sam: 3 Phil: 26 Jill: 10 Team 9 Bill: 19 John: 7 Linda: 15 So, let's get started. Like every Perl program, we start with a shebang line and three lines to enable modern features, warnings, and strictures. Bon!, démarrons. Comme tout programme Perl, on commence avec une ligne shebang et trois lignes pour activer les nouvelles caractéristiques, les avertissements et restrictions. 1 #!/usr/bin/env perl 2 3 use 5.010000; 4 use warnings; 5 use strict; Then, we need to open the input file and read through it until we find the section for our team. 7 my $team_number = 42; 8 my $filename = 'input.txt'; 9 10 open(my $fh, '<', $filename) or die "cannot open '$filename' $!"; 11 12 my $found; 13 while(<$fh>) { 14 if(m/^Team (\d+)$/) { 15 next if($1 != $team_number); 16 $found = 1; 17 last; 18 } 19 } 20 die "cannot find 'Team $team_number'" unless($found); This handful of code introduces several details about perl. Let's step through each part and see what is happening. On lines 7 and 8, we're only declaring some variables.