in reply to return undef

Consider raising an exception (with die) when the error happens. Using the return value of a sub for both data and error signalling tends to always have this problem: using exceptions makes error conditions out-of-band and lets you treat the error when it's most convenient for you.

I was about to give an example, but then I realized I don't understand what myfunction() is doing and who opens FILEHANDLE.

Replies are listed 'Best First'.
Re^2: return undef
by Thilosophy (Curate) on May 23, 2005 at 04:38 UTC
    Another useful feature of raising exceptions is that you can pass along the actual error message, which makes debugging much easier.
    open $fh, ">$filename" or die "could not open '$filename' for writing: $! "
    Also, if you do not write code to catch the exception, your program will terminate with a more meaningful message than "Use of uninitialized value at ... ".
      die now takes more than just strings. You can use first-class objects if you like. This lets you add more structured information about the exception, which also makes catch/propagate decisions less kludgy to implement.