Others have told you what's wrong with your code, but I'd like to mention that there are other options besides a flag value and a subroutine. In particular, I would suggest something like
unless(open FILE, ">test.txt"){
#error handling code goes here
}