B
Bill H
Just to preface the following so I don't get the "daily WTF" or
comments on code, I am not a "perl guru", I write code everyday in
perl, and though good at it, I will be the 1st to admit I don't know
everything.
I see examples like this in many programs, where you try to open a
file for write and you have a "die" statement after it:
open(TMP, '>'.$file) or die "Can't create file";
My question is, why would you use the "die" statement, ESPECIALLY in
the context of handling web based requests?
If you are controlling all of the inputs, filename, permissions, etc
and using flock, what is the need for a "die" statement?
It is not accomplishing anything, other than telling you that it could
not open the file, but again, if you control all the parameters then
it should be able to open the file everytime. If not, then it would
seem that the programmer did not anticipate everything that could stop
his program from working as expected.
Also, when perl is used as web based program, the die "some text"
isn't going to do anything except trigger a Server Error because you
do not have it in an HTML wrapper, unless you are using Carp, or it is
putting it in some log for later viewing (or I could be off on this,
if so let me know). Again, in the context of a web based program, the
die "some text", if shown to a visitor in a browser, is not going to
help the programmer, and may just help someone figure out how to hack
your program (especially if you are showing directory structures (ex:
die "Could not write $file /home/sites/www.domain.com/users/
webmaster")
It would seem more logical to have some error recovery routine instead
of a simple die. Somethig like:
open(TMP, '>'.$file) or try &errorHandlingRoutine;
Where if there was a failure it would execute the
&errorHandlingRoutine to attempt to fix the problem and then restart
the same line. The &errorHandlingRoutine could then "die" if it
couldn't fix it.
The perldocs say that you can have die almost do as I suggested, but
you can not return to the same line to try again.
Now knowing perl and the millions of module available out there,
something similar probably exists, but, if it does I have not seen
it.
Bill H
comments on code, I am not a "perl guru", I write code everyday in
perl, and though good at it, I will be the 1st to admit I don't know
everything.
I see examples like this in many programs, where you try to open a
file for write and you have a "die" statement after it:
open(TMP, '>'.$file) or die "Can't create file";
My question is, why would you use the "die" statement, ESPECIALLY in
the context of handling web based requests?
If you are controlling all of the inputs, filename, permissions, etc
and using flock, what is the need for a "die" statement?
It is not accomplishing anything, other than telling you that it could
not open the file, but again, if you control all the parameters then
it should be able to open the file everytime. If not, then it would
seem that the programmer did not anticipate everything that could stop
his program from working as expected.
Also, when perl is used as web based program, the die "some text"
isn't going to do anything except trigger a Server Error because you
do not have it in an HTML wrapper, unless you are using Carp, or it is
putting it in some log for later viewing (or I could be off on this,
if so let me know). Again, in the context of a web based program, the
die "some text", if shown to a visitor in a browser, is not going to
help the programmer, and may just help someone figure out how to hack
your program (especially if you are showing directory structures (ex:
die "Could not write $file /home/sites/www.domain.com/users/
webmaster")
It would seem more logical to have some error recovery routine instead
of a simple die. Somethig like:
open(TMP, '>'.$file) or try &errorHandlingRoutine;
Where if there was a failure it would execute the
&errorHandlingRoutine to attempt to fix the problem and then restart
the same line. The &errorHandlingRoutine could then "die" if it
couldn't fix it.
The perldocs say that you can have die almost do as I suggested, but
you can not return to the same line to try again.
Now knowing perl and the millions of module available out there,
something similar probably exists, but, if it does I have not seen
it.
Bill H