到目前为止,我已成功通过打开输出文件作为外部循环的一部分并在写入所有输出后关闭它来生成单个文件的输出。我曾使用计数变量($ x)并在其上附加.txt来创建文件名,并将其写入与perl脚本相同的目录中。我想稍微提高代码,提示用户输入文件名,只打开一次文件,然后每页输出一个“打印字母”。这可能是纯文本吗?根据我的理解,chr(12)是一个ascii换行角色,会让我接近我想要的东西,但有更好的方法吗?先谢谢你,伙计们。 :)
sub PersonalizeLetters{
print "\n\n Beginning finalization of letters...";
print "\n\n I need a filename to save these letters to.";
print "\n Filename > ";
$OutFileName = <stdin>;
chomp ($OutFileName);
open(OutFile, ">$OutFileName");
for ($x=0; $x<$NumRecords; $x++){
$xIndex = (6 * $x);
$clTitle = @ClientAoA[$xIndex];
$clName = @ClientAoA[$xIndex+1];
#I use this 6x multiplier because my records have 6 elements.
#For this routine I'm only interested in name and title.
#Reset OutLetter array
#Midletter has other merged fields that aren't specific to who's receiving the letter.
@OutLetter = @MiddleLetter;
for ($y=0; $y<=$ifLength; $y++){
#Step through line by line and insert the name.
$WorkLine = @OutLetter[$y];
$WorkLine =~ s/\[ClientTitle\]/$clTitle/;
$WorkLine =~ s/\[ClientName\]/$clName/;
@OutLetter[$y] = $WorkLine;
}
print OutFile "@OutLetter";
#Will chr(12) work here, or is there something better?
print OutFile chr(12);
$StatusX = $x+1;
print "Writing output $StatusX of $NumRecords... \n\n";
}
close(OutFile);
}
答案 0 :(得分:1)
将“页面”与换页符分开,但您必须在每个页面后执行,而不是在结尾处。我不确定PersonalizeLetters
应该做什么,但看起来你用什么来打印所有的字母。在那种情况下,我认为你只需要重新调整一下。在子例程外部进行所有设置,传入文件名,然后为每条记录执行您需要执行的操作。处理完记录后,打印换页:
sub PersonalizeLetters
{
my( $OutFileName ) = @_;
open my $out, '>', $OutFileName
or die "Could not open $OutFileName: $!";
for( $x=0; $x < $NumRecords; $x++ )
{
print "Writing output $x of $NumRecords...\n\n";
print $out $stuff_for_this_record;
print $out "\f";
}
}