utilisation de text::csv pour le parsing

recuperation en tete de colonne
modifications des entrées sans conditions
This commit is contained in:
2019-10-16 19:35:04 +02:00
parent 9b1a298478
commit fffebbd771

28
test.pl
View File

@@ -5,6 +5,7 @@
# d'un fichier csv passé en argument
use strict;
use Net::LDAP;
use Text::CSV;
my $ldapserver = "localhost";
my $binddn = "cn=manager,o=od";
@@ -17,21 +18,32 @@ my $mesg = $ldap->bind( $binddn,
);
$mesg->code and die $mesg->error; # check for errors
my $csv = Text::CSV->new({ sep_char => ';' }) ;
my $file = $ARGV[0] or die "Need to get CSV file on the command line\n" ;
open(my $data, '<', $file) or die "Could not open '$file' $!\n";
while (my $line = <$data>) {
chomp $line;
my @fields = split ";" , $line;
my $dn = $fields[0] ;
my $attr1 = $fields[1] ;
my @cols = @{$csv->getline ($data)};
foreach my $c (@cols){
print "columne: ". $c ."\t" ;
}
my $row = {} ;
$csv->bind_columns (\@{$row}{@cols});
while ($csv->getline ($data)) {
my $dn = $row->{dn} ;
my $attr1 = $row->{seeAlso} ;
my $attr2 = $row->{l} ;
# my $mesg = $ldap-> search(
# base=> $dn,
# scope=> "base",
# filter=> "(description=".$attr1.")") ;
# print $mesg->count() ;
# if ($mesg->count() != 0) { next ; }
print( $dn."\t".$attr1."\n" ) ;
my $mesg = $ldap-> modify( $dn,
changes=> [
delete => [ 'description' ],
add => [ description => $attr1 ]
replace => [ 'description'=> $attr1 ],
replace => [ 'l' => $attr2 ],
]
) ;
$mesg->code and warn $mesg->error; # check for errors