Need help with grep command in Perl

I am writing a Perl script that uses the grep command and instead of writing to the output file it just writes to the screen.

#!/usr/bin/perl
#
#
@ffiles = `ls CP*BUFFER*.dat`;

foreach my $file (@ffiles){
@data = split(/./, $file);
$filename = $data[0];
$extension = $data[1];

#print "$filename\n";

@newfile = split(/_/, $filename,2);
$CP = $newfile[0];
$name = $newfile[1];

#print "$name\n";

$outfile = "CF.dat";

#print "$outfile\n";
#open(OUTFILE, ">$outfile");

system("grep  FL $file > output.dat ");
system("grep AFB $file >> output.dat");
system("mv output.dat $outfile");
}

Thanks to whoever can help:)

you want to capture the output of the grep command and write it to the end a file, yes?
You can do that within the system calls as follows

system("grep  FL $file >> $outfile ");
system("grep AFB $file >> $outfile");

...am I missing something? Your 20-line perl script, which uses four shells, amounts to a single-line shell script:

egrep -h "FL|AFB" CP*BUFFER*.dat > CF.dat