Last active
October 21, 2015 23:56
-
-
Save masterT/48f7fd03fc1b322bd044 to your computer and use it in GitHub Desktop.
Lab 4 Intro au language Perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
my $a = 'foo'; | |
my $b = 'brack'; | |
my $une_condition = 1; | |
if ($une_condition) { | |
my $b = "bar"; | |
print $a; # Affiche "foo" | |
print $b; # Affiche "bar" | |
print "\n"; | |
} | |
print $a; | |
print $b."\n"; | |
my $c = 1; | |
my $d = 0; | |
if ($d) { | |
print "d = $d\n"; | |
} elsif ($a) { | |
print "c = $c\n"; | |
} else { | |
print "rien\n"; | |
} | |
unless ($d) { | |
print "c'est équivalent à if (!$d)\n"; | |
} | |
# boucle while | |
my $i = 0; | |
while ($i <= 10) { | |
print "$i\n"; | |
$i++; | |
} | |
# boucle for | |
my @tab; | |
for ($i=0; $i <= 10; $i++) { | |
$tab[$i] = $i + 10; | |
print "tab[$i] = ".($i + 10)."\n"; | |
} | |
foreach (@tab) { | |
print "L'élément courant est $_ \n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
open(OUTFILE, ">output.txt") or die "Impossible d’ouvrir output.txt en écriture : $!"; | |
my @tab = (1,2,3,4); | |
my $i = 0; | |
foreach (@tab) { $_ = $i+10; | |
print OUTFILE "Ligne ".($i+1)." = $_\n"; | |
$i++; | |
} | |
close(OUTFILE); | |
open(INFILE, "output.txt") or die "Impossible d’ouvrir input.txt en lecture : $!"; | |
my @tab2 = <INFILE>; | |
foreach (@tab2) { | |
print $_; | |
} | |
close (INFILE); | |
print "Une autre façon d'afficher le contenu d'un fichier\n"; | |
open(INFILE, "output.txt") or die "Impossible d’ouvrir input.txt en lecture : $!"; | |
while (<INFILE>) { | |
print $_; | |
} | |
close(INFILE); | |
open(LOGFILE, ">>output.log") or die "Impossible d’ouvrir logfile en ajout : $!"; | |
print "Entrez des caractères : "; | |
my $var = <STDIN>; | |
while ($var ne "quit\n") { | |
print LOGFILE $var; | |
$var = <STDIN>; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# Retourn l'entrée standard sans le dernier caractère "\n" | |
sub stdin{ | |
my $in = <STDIN>; | |
chop($in); | |
return $in; | |
} | |
sub retour_au_menu { | |
print "Appuyer sur <Entrée> pour retourner au menu..."; | |
stdin(); | |
print "\n"; | |
} | |
# Options du menu | |
# 1. Créer un fichier | |
# 2. Lire un fichier | |
# 3. Ajouter du contenu à un fichier | |
# 4. Quitter | |
my $in = ""; | |
while ($in ne "4") { | |
print "\n"; | |
print "************ Menu ************\n"; | |
print "1. Créer un fichier\n"; | |
print "2. Lire un fichier\n"; | |
print "3. Ajouter du contenu à un fichier\n"; | |
print "4. Quitter\n"; | |
print "Votre choix : "; | |
$in = stdin(); | |
print "\n"; | |
# créer un fichier | |
if ($in eq "1") { | |
print "Spécifier le nom du fichier à créer : "; | |
my $file_name = stdin(); | |
open(FILE, ">", $file_name) or die "Impossible de créer le fichier ". $file_name ."$!"; | |
close(FILE); | |
print "Fichier créé!\n"; | |
retour_au_menu(); | |
} # lire un fichier | |
elsif ($in eq "2") { | |
print "Spécifier le nom du fichier à lire : "; | |
my $file_name = stdin(); | |
open(FILE, "<", $file_name) or die "Impossible d'ouvrir le fichier ". $file_name ."$!"; | |
while (<FILE>) { | |
print $_; | |
} | |
close(FILE); | |
retour_au_menu(); | |
} # lire un fichier | |
elsif ($in eq "3") { | |
print "Spécifier le nom du fichier auquel on ajout le contenu : "; | |
my $file_name = stdin(); | |
open(FILE, ">>", $file_name) or die "Impossible d'ouvrir le fichier ". $file_name ."$!"; | |
print "Écrier le contenu à ajouter. Pour quitter 'fin' puis appuyer sur <Entrée>.\n"; | |
my $in = <STDIN>; | |
while ($in ne "fin\n") { | |
print FILE $in; | |
$in = <STDIN>; | |
} | |
close(FILE); | |
retour_au_menu(); | |
} # quitter | |
elsif ($in eq "4") { | |
exit(1); | |
} # invalid | |
else { | |
print "Option '". $in ."' est invalid!\n"; | |
retour_au_menu(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment