in reply to Trouble making my own open()-like routines.

You probably need, for one thing, to pass the typeglob, not the bareword filehandle. I understand the prototypes, but they need to come before the sub is ever called... and prototypes are an ugly world anyway. Even better, pass a lexical just as you would if you said, "open my $fh, $filename or die $!;".

And next, you should probably be using the three arg version of open. That's safer.

And next... you should probably die instead of just returning if you fail to open the file. Currently, when you fail to open the file, you'll never know why. You could die, print $! for details on why you failed.


Dave

Replies are listed 'Best First'.
Re^2: Trouble making my own open()-like routines.
by ysth (Canon) on Jul 07, 2004 at 15:30 UTC
    I think this is a case where the * prototype is justified. The OP is using it (in conjunction with Symbol::qualify_to_ref) correctly.

    With a space after the > or >+, two arg open is safe.

      I'd much prefer to use the three-argument version of open, but sadly, my target platform is Perl 5.005 and the three-argument version of open was introduced with 5.6.1.

      I also don't want to die in the routines themselves because that would limit the amount of information in the error messages. Far better to let the caller handle it, so they can do stuff like this:

      openFileForWriting($fh, 'file.log') or die "Couldn't open log file to store the crash report: $!";