#REPLACE_1 #!/usr/bin/perl #print "Content-type:text/html\n\n"; open(FICHIER, ") { $ligne =~ s/[.]/ /g; print $ligne; } close FICHIER; #--------------------------------------------------- #REPLACE_2 #!usr/bin/perl #print "Content-type:text.html\n\n"; open(FICHIER, ") { #supprimer les caractères entre les crochets: s/[\;><.]+/ /g; print $_; } close FICHIER; #---------------------------------------------- #REPLACE_3 #!/usr/bin/perl print "Content-type:text.html\n\n"; $ft="Lindon"; #s/i/o/g : à toutes les occurrences #s/i/o/gi : i pour "ignore" casse (maj ou min) $ft =~ s/i/o/; print "$ft "; #$ft now equals London. exit; #----------------------------------------------- #REPLACE_4 #!/usr/bin/perl print "Content-type:text.html\n\n"; $mystring = "hello, perl"; print $mystring."
"; substr($mystring, 7) = "world"; print $mystring."
"; #résultat: #hello, perl #hello, world $mystring = "hello, perl, titi, toto"; print $mystring."
"; substr($mystring, 9, 11) = ""; print $mystring."
"; #résultat: #hello, perl, titi, toto #hello, peoto #-------------------------------------------------------------- #SEARCH #!/usr/bin/perl print "Content-type:text/html\n\n"; open(FILE, "; close(FILE); #print @lignes; $nom = translator; for ($i=0; $i<@lignes; $i++) { if($lignes[$i] =~ /$nom/) { print "$nom est à la ligne $i\n"; } } #--------------------------------------------------------- #OPEN AND WRITE_1 #!/usr/bin/perl print "Content-type:text.html\n\n"; open(FICHIER, "decode.txt") or die "Le fichier ne peut être édité !\n"; $i=0; while ($ligne = ) { #écrire 10 lignes: if($i<10) { print WRITER $ligne; } $i++; } close FICHIER; close WRITER; #lire les 10 lignes dans "decode.txt": open(FICHIER, ") { print $ligne; } close FICHIER; #-------------------------------------------------------------------- #OPEN AND WRITE_2 #!/usr/bin/perl print "Content-type:text/html\n\n"; open(DA, ">places.txt"); #you can print anything you want to a file handle : text, variables, lists, you name it ! #print FILEHANDLE "I see London"; #print FILEHANDLE "$name"; #print FILEHANDLE @list; #print FILEHANDLE %hash; print DA "I see London\n"; close(DA); open(DA, "; close(DA); print @da; exit; #-------------------------------------------------------------- #OPEN AND WRITE_3 #!/usr/bin/perl print "Content-type:text.html\n\n"; open(TEXT, ">text"); print TEXT "Bonjour le monde.\n"; close(TEXT); #------------------------------------------------------------ #OPEN AND READ_1 #!/usr/bin/perl open(FICHIER, ") { print $ligne; } close FICHIER; #------------------------------------------------ #OPEN AND READ_2 #!/usr/bin/perl print "Content-type:text.html\n\n"; open (READ, "data.txt") || die "can't open"; while () { print "$_"; } close READ; #----------------------------------------------------- #OPEN AND READ_3 #!/usr/bin/perl -w print "Content-type:text.html\n\n"; open(FILE, "; close(FILE); print @lignes; #-------------------------------------------------------- #OPEN AND READ_4 #!/usr/bin/perl print "Content-type:text/html\n\n"; #contenu de : cities.txt : #London #Paris #Tokyo open(DA, "; close(DA); print $huhu[2]; exit; #---------------------------------------------------- #OPEN AND READ_5 (ligne par ligne): #!/usr/bin/perl print "Content-type:text.html\n\n"; @lignes = (""); open(FILE, ") { push(@lignes,$_); } close(FILE); for(@lignes) { print $_; } #----------------------------------------------------- #OPEN AND READ_6 #!/usr/bin/perl print "Content-type:text.html\n\n"; open (F, "my_file.txt"); undef $/; $contenu_fichier = ; close(F); print $contenu_fichier; exit; #------------------------------------------------------ #OPEN AND READ_7 open( MON_FICHIER, " ) { chomp( $ligne ); print STDOUT "$ligne\n"; } close( MON_FICHIER ); #---------------------------------------------------- #FOREACH #!/bin/perl open(FLUX, "fichier"); @LIGNES = ; close(FLUX); print("accès en liste : \n"); $i = 0; foreach $ligne (@LIGNES) { print("LIGNE[$i] : $ligne"); $i++; } #résultat: accès en liste : LIGNE[0] : première ligne LIGNE[1] : deuxième ligne LIGNE[2] : troisième ligne #------------------------------------ # foreach avec grep open(DA, "; close(DA); @resultat = grep(/pattern/, @LIGNES); $i = 0; foreach $ligne (@resultat) { print("LIGNE[$i] : $ligne"); $i++; } #------------------------------------------------------------ #push - pop : travaillent sur la fin d'un tableau open(F, "; close(F); push(@liste, "ligne rajoutée"); #"ligne rajoutée" est ecrit dans 'data.txt print @liste; pop(@liste); #"ligne rajoutée" est supprimée de 'data.txt #------------------------------------------------------- #unshift() : ajoute au début d'un tableau #shift() : supprime le début d'un tableau #------------------------------------------------------ $file = "hello.txt"; open(GREETING, $file); #is the same that: $file = "hello"; open(GREETING, "$file.txt"); #-------------------------------------------------- #IF AVEC PLUSIEURS CONDITIONS: #!/usr/bin/perl print "Content-type:text.html\n\n"; open (FILE, ") { $pattern1 = "