Have a try with:
#!/usr/bin/perl
use strict;
use warnings;
my $infile = 'filetest.txt';
my $outfile = 'testfile.txt';
# use 3-arg open and if open succeeded
open my $fh_in, '<', $infile or die "Unable to open file '$infile' for reading: $!";
my @with_test;
my @without_test;
while (<$fh_in>) {
if (/test/) {
push @with_test, $_;
} else {
push @without_test, $_;
}
}
close $fh_in;
open my $fh_out, '>', $outfile or die "Unable to open file '$outile' for writting: $!";
print $fh_out "Lines with test\n";
print $fh_out @with_test;
print $fh_out "Lines without test\n";
print $fh_out @without_test;
close $fh_out;