précédant | suivant | sommaire

ldif2rp.pl      _      Sortir deux rapports formatés d'un fichier .ldif      _        par Alain Adelmar

Faire des étiquettes prédécoupables avec votre carnet d'adresses de netscape, c'est à dire, sortir deux rapport, un à l'écran l'autre dans un fichier texte. Ce fichier sera formater en vue d'etre imprimer puis découper.
Sorti de là, cet exemple montre plusieurs manières de faire des formats et des routines, d'utiliser printf.
alain


#!/usr/bin/perl
# remd: extrait un rapport d'un fichier .ldif - alain Adelmar 20 fév 2001.
# revue pour le format etiquettes sur file en Juin 2002. fonctionne
$x = "x" x 15;
$rf = "etq_adresses.txt";
$trait = "*" x 72;
$tiret = "_" x 72;

LABEL_TOP:print "Entrez le nom du fichier à traiter:\n";
chomp($file = <STDIN>);
$u = 0# initialisation de boucle spéciale pour les groupes de noms

if (-e $file) {
 $df = localtime(-C $file);
 print "\n$x\nRapport du contenu de $file $df\n";
 }
else {
 print "Erreur, le fichier $file n\'existe pas\n";
 goto LABEL_TOP;
 }
open F, "$file" or die "Ouverture de $file impossible $!";
open RF, ">$rf" or die "Ouverture du rapport formate impossible $!";
foreach $line(<F>) {
  # $u seulement pour les liste de listes ou l'on ne prend que nom et email
  if ($u == 1) {
    if ($line =~ /^member/) {
      # on defini les deux champs nom et adresse_mail
      ($nomp, $mailp) = split(/,/,$line);
      ($kk, $nom)= split(/=/, $nomp);
      ($kk, $mail)= split(/=/, $mailp);
      chomp($mail);
      # on écrit nom et e_mail dans le format (à voir si c'est utile?)
      write;
      printf RF "Nom: %25s | e_mail: %30s\n", $nom, $mail;
    }
    else {
      # sortie de la boucle des listes de listes
      $u = 0;
 
    }
  }
 
  # $i++ if (length($line)== 0); # ou # next if (length($line)==0);
 
  elsif ($line =~ /^dn:\scn=/) {
    chomp $line;
    ($nom, $mail) = split(/,/,$line);
    $nom =~ s/^dn:\scn=()/$1/;
    $mail =~ s/mail=()/$1/;
    chomp $mail;
  }
  elsif ($line =~ /^mail:/) {
    chomp $line;
    &nettoyage($line);
    $mail2 = "$mm";
  }
  elsif ($line =~ /^sn:/) {
    chomp $line;
    &nettoyage($line);
    $sn ="$mm";
  }
  elsif ($line =~ /^telephonenumber:/) {
    chomp $line;
    &nettoyage($line);
    $tel = "$mm";
  }
  elsif ($line =~ /^o:/) {
    chomp $line;
    &nettoyage($line);
    $org = "$mm";
 
  }
  elsif ($line =~ /^locality:/) {
    chomp $line;
    &nettoyage($line);
    $loc = "$mm";
  }
  elsif($line =~ /^postalcode:/) {
    chomp $line;
    &nettoyage($line);
    $pc_ad = "$mm";
  }
  elsif($line =~ /^streetaddress:/) {
    chomp $line;
    &nettoyage($line, $m);
    $st_ad = "$mm";
  }
  elsif ($line =~ /^homephone/) {
    chomp $line;
    &nettoyage($line);
    $h_tel ="$mm";
  }
  elsif($line =~ /^title:/) {
    chomp $line;
    &nettoyage($line);
    $title = "$mm";
  }
  elsif ($line =~ /^st:/) {
    chomp $line;
    &nettoyage($line);
    $reg_ad = "$mm";
  }
  elsif($line =~ /^objectclass:\sperson/)  {
      write;
      $noms[$i]= (["$nom", "$mail", "$mail2", "$tel", "$sn", "$loc", "$pc_ad", "$st_ad", "$reg_ad", "$org"]);
      formatage4rapport("$nom", "$mail", "$mail2", "$tel", "$sn", "$loc", "$pc_ad", "$st_ad", "$reg_ad", "$org");
      push @liste, @noms;
      $nom = $mail = $mail2 = $tel = $sn = $loc = $pc_ad = $st_ad = $reg_ad = $org = 0;
      $i++;
 }
  elsif($line =~ /^objectclass:\sgroupOfNames/) {
    $u = 1;
  }
  $n = $i;
  # pour chaque ligne fini passe par ici donc

}

 
 

close F or die "Fermeture impossible: $file $!";
close RF or die "Fermeture du rapport impossible $!";

for($a=0;$a>=$n;$a++) {
  print $noms[$a];
}

sub nettoyage($line,$mm) {
  my($line)= @_;
 
  ($kk, $mm)= split(/:/, $line);
  $mm =~ s/^\s()/$1/;
  chomp $mm;
  return $mm;
}

sub formatage4rapport($nom, $mail, $mail2, $tel, $sn, $loc, $pc_ad, $st_ad, $reg_ad, $org) {
  my($nom, $mail, $mail2, $tel, $sn, $loc, $pc_ad, $st_ad, $reg_ad, $org)=@_;
  print RF "$trait\n";
  printf RF "Nom: %25s || mail: %32s\n", $nom, $mail;
  print RF "$tiret\n";
  printf RF "surnom: %21s || mail2: %31s\n", $sn, $mail2;
  print RF "$tiret\n";
  printf RF "tel: %14s | tel_maison: %14s\n", $tel, $h_tel;
  printf RF "org: %14s | titre: %14s\n", $org, $title;
  print RF "$tiret\n";
  printf RF "adresse: %20s | localite: %10s | CP: %6s\n", $st_ad, $loc, $pc_ad;
  printf RF "reg_adr: %10s\n", $reg_ad;
  print RF "$trait\n";
}
 
format STDOUT =
========================================================
| @<<<<<<<<<<<<<<<<<<<<  | @<<<<<<<<<<<<<<<<<<<<<<<<<< |
$nom, $mail
| @<<<<<<<<<<<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<<<<<  |
$mail2, $org
| @<<<<<<<<<<<<<<<<<<<<  | @<<<<<<<<<<<<<<<  CP:@<<<<< |
$sn, $tel, $pc_ad
| @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  @<<<<<<<<<<<<<<<<<< |
$st_ad, $loc
| @<<<<<<<<<<<<<<<<<<                                  |
$reg_ad
========================================================
.
 
 
 
 

END;