in reply to how to write a subroutine content into a file

A simple way to correct your sample code would be to replace:

print "How are you?\n";

with:

return "How are you?\n";

Now, your subroutine is returning a string, so your print statement below will print the string itself rather than the results of your successful print statement.

Edit: Oops, I didn't see the reply above until after I posted this.