in reply to capturing stderr of echo piped to a file
Your redirection is in the wrong order:
my $command = "echo $writeline >> $file"; my $output = `$command 2>&1`;
Writing that out, you end up with
my $output = `$command echo $writeline >> $file 2>&1`;
Which means redirect STDOUT to the file, then STDERR to (current) STDOUT, i.e. also to the file. You probably want
my $output = `$command echo $writeline 2>&1 >> $file`;
instead, i.e. redirect STDERR to STDOUT, then STDOUT (but not the redirected STDERR) to the file.