Following my other question here..
I have the following piece of code where I read the contents of a webpage line by line, trying to match a certain pattern and if matched, write it to a file:
foreach my $line (split qr/\R/, $mech->content) {
if ($line=~ m/t\/([A-Z]+)/){
print $fileHandle "$1\n";
}
}
I wonder whether it possible to append the matching lines to another multi-line variable and write it to a file only when the loop is finished.
The reason I want it to be that way is because I want to use the following subroutine to save data to a file, rather than doing it directly:
writeToFile("fileName.tmp","path",data);
This is a subroutine I wrote, which apart of just saving the data in a file, also checks the following:
- Whether the specified path already exists (and creates it if needed)
- If the file already exist on disk, compare it to the one which is about to be written (and write it only in case the files are different)
So if I'll be able to create this additional variable, I will have one less file to write to a disk.