my $outfile = 'temp.txt'; open(my $fh, '>', $outfile) or die "Cannot open file '$outfile' for writing: $!"; print $fh find_student('x3')->display(); close($fh) or die "Cannot close file '$outfile': $!";