Thanks Ken for your below link to tie and your answers.
The reason I created my program in the above way
Is so it only replaces the first line in the loop
not every line it loop . It seems crazy but I need this in
the program I am creating.
If it's only for the first line, use this instead:
use strict;
use warnings;
use Tie::File;
tie my @xfil, 'Tie::File', 'var.txt' or die $!;
$xfil[0] =~ s/the line to be replaced/this is the new line/;
untie @xfil;