Newsgroup: it.comp.lang.perl
Titolo: Help parsare file ed inserire i dati in DB Mysql

Ho un file tipo:
Revoked Certificates:
     Serial Number: 0325DD
         Revocation Date: Jun 30 10:00:31 2008 GMT
         CRL entry extensions:
             Invalidity Date:
                 Jun 30 00:00:00 2008 GMT
             X509v3 CRL Reason Code:
                 Cessation Of Operation
     Serial Number: 03267A
         Revocation Date: Jun 30 10:49:25 2008 GMT
         CRL entry extensions:
             Invalidity Date:
                 Jun 30 00:00:00 2008 GMT
             X509v3 CRL Reason Code:
                 Cessation Of Operation
     Serial Number: 0326CA
         Revocation Date: Jun 30 11:23:29 2008 GMT
         CRL entry extensions:
             Invalidity Date:
                 Jun 30 00:00:00 2008 GMT
             X509v3 CRL Reason Code:
                 Superseded
     Serial Number: 032749
Dovrei inserire i relativi campi nel DB ma mi incasino con i cicli,
l'idea è faccio un open() splitto su /Serial Number:/ suddividendo in 
blocchi da parsare per poi sparare in insert le variabili, e fin qui la 
teoria ma in pratica come leggo il file splittandolo?

Ecco lo script incriminato:
#!/usr/bin/perl
open(CRL, "CRL.txt") or die("$!");
while ($line = <CRL>){
my @rows = split('Serial ', $line);
foreach my $val (@rows) {
  if($val =~ s/Number://)
         {
         $val =~ s/^\s+|\s+$//g;
         $serial_number = $val;
         }
if($val =~ s/Revocation Date://)
         {
         $val =~ s/^\s+|\s+$//g;
         $revocation_date = $val;
         }
if($val =~ /Invalidity Date:/)
         {
         my @data = split(/\|/, $val);
         $nextline = <CRL>;
         $nextline =~ s/^\s+|\s+$//g;
         $invalidity_date = $nextline;
         }
if($line =~ /X509v3 CRL Reason Code: /)
         {
         my @data = split(/\|/, $val);
         $nextline = <CRL>;
         $nextline =~ s/^\s+|\s+$//g;
         $reason_code = $nextline;
         }
print "$serial_number | $revocation_date | $invalidity_date | 
$reason_code |\n";
         }
}
close(CRL);
e l'output
094E3E | Jun 30 13:36:30 2011 GMT | Jun 30 13:36:00 2011 GMT | 
Affiliation Changed |
094E3E | Jun 30 13:36:30 2011 GMT | Jun 30 13:36:00 2011 GMT | 
Affiliation Changed |
094E3E | Jun 30 13:36:30 2011 GMT | Jun 30 13:36:00 2011 GMT | 
Affiliation Changed |
094E3E | Jun 30 13:36:30 2011 GMT | Jun 30 13:36:00 2011 GMT | 
Affiliation Changed |
094E3E | Jun 30 13:36:30 2011 GMT | Jun 30 13:36:00 2011 GMT | 
Affiliation Changed |
094E3E | Jun 30 13:36:30 2011 GMT | Jun 30 13:36:00 2011 GMT | 
Affiliation Changed |
evidentemente il foreach all'interno di un while non ï¿œ una buona idea e 
fin qua ci sono arrivato.

 Cessation Of Operation
Prova con
use strict;	
use warnings;
open my $input, '<', 'input.txt' or die "$!";
my @linee = <$input>;
chomp(@linee);
my %hash_record=();
foreach my $indice (0 .. $#linee) {
	my $linea = $linee[$indice];
 
	if ($linea =~ /Serial Number: (.*?)$/) {
		$hash_record{sn} = $1;
 
	} elsif ($linea =~ /Revocation Date: (.*?)$/) {
 
		$hash_record{rd} = $1;
	} elsif ($linea =~ /Invalidity Date:/) {
		my $id = $linee[$indice+1];
		$id =~ s/^\s*(.*?)\s*$/$1/;
		$hash_record{id} = $id
	} elsif ($linea =~ /X509v3 CRL Reason Code:/) {
		my $crl = $linee[$indice+1];
		$crl =~ s/^\s*(.*?)\s*$/$1/;
 
		print "$hash_record{sn} | $hash_record{rd} | $hash_record{id} | $crl |\n";
	}
}