Perl open()
Dear perl programmers,
When using open(), please don’t use:
open FILE, "$file" or die "couldn't open filen";
It really helps if you tell us what file you’re trying to open and what went wrong. The correct error message is:
open FILE, "$file" or die "could not open $file: $!n";
Thank you.
9 comments
Comments are closed. These are preserved from the original site.
On a side note: can I ask the stupid question where PivoxyWindowOpen comes from? Google gives too many false positives.
On a side note: can I ask the stupid question where PivoxyWindowOpen comes from? Google gives too many false positives.
You probably want to read http://mail.python.org/pipermail/python-list/2003-August/178599.html
That should be addressed to all programmers, not just the perl hackers. We had a lot of trouble with these kind of error messages when we first had to install a software which acquires data from a lab machine. The installation was on a networked pc running windows and should of course only run with user rights. The only error message we got when we started the program as a user was that it couldn't access some file (not giving the path nor the file name of the file). This kind of problem is much harder to trace down in a windows environment with compiled software than using Linux and perl scripts..
open my $file, "filename" or die "Couldn't open filename: $!"
since "my $file" will limit the scope of the handle.
Aren't we all using IO::File these days? :)
Besides, this is the EXACTLY the same as
my $file = IO::File->open("filename") or die "Couldn't open filename: $!"
open FILE, "<$file" or die "couldn't open file: $!n";
Otherwise it could be a security vulnerability, if $file is controlled by a remote user and starts with a "|" character.
Also, tainting should protect you against using user data in an open command.