perl でテキスト ファイルを処理しようとしています。ファイルのデータをデータベースに保存する必要があります。私が抱えている問題は、いくつかのフィールドに改行が含まれていることです。これらのフィールドを含める最良の方法は何ですか?
data.txt ファイルの例:
ID|Title|Description|Date
1|Example 1|Example Description|10/11/2011
2|Example 2|A long example description
Which contains
a bunch of newlines|10/12/2011
3|Example 3|Short description|10/13/2011
現在の (壊れた) Perl スクリプト (例):
#!/usr/bin/perl -w
use strict;
open (MYFILE, 'data.txt');
while (<MYFILE>) {
chomp;
my ($id, $title, $description, $date) = split(/\|/);
if ($id ne 'ID') {
# processing certain fields (...)
# insert into the database (example)
$sqlInsert->execute($id, $title, $description, $date);
}
}
close (MYFILE);
例からわかるように、ID 2 の場合、未定義の変数を参照しようとするとエラーが発生するいくつかの行に分割されます。それらをどのように正しいフィールドにグループ化しますか?
前もって感謝します!(質問が十分に明確で、タイトルを定義するのが難しいことを願っています)