in reply to testing a croak
Use block eval
Perl> sub thatDies{ die "Kamakazi\n"; };;
Perl> print "Test failed with $@\n" unless eval{ thatDies() };;
Test failed with Kamakazi
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: testing a croak
by jbrugger (Parson) on Jan 19, 2006 at 06:05 UTC
|
| [reply] |
|
Eval creates major security issues if improper used.
Wrong. That problem only exists with the string form of eval, not the block form shown, which is the standard Perl equivalent of the try()/catch() pairing found in other languages. Ie. it is the proper way to do this.
2. I don't like die
Your prerogative, but it is a standard language facility and deciding you don't like it is arbitrary. Also, remember that not all Perl programs are "web applications".
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
|
Wrong. That problem only exists with the string form of eval, not the block form shown, which is the standard Perl equivalent of the try()/catch() pairing found in other languages. Ie. it is the proper way to do this.
I know you're using it the proper way, but i only state to be careful with it and that you absolutely should know what you're doing, (As stated if improper used.. Thats why i try to avoid the usage of it, a mistake is easily made like this:
#!/usr/bin/perl -w
use stritct;
my $c1 = 'system("touch /tmp/test1.txt")';
my $c2 = 'system("touch /tmp/test2.txt")';
#used as string (Dangerous!!!)
eval $c1;
#used as block (ok)
eval {$c2}; # <- gives a useless use of private variable in void cont
+ext.
.
"We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
| [reply] [d/l] |
|
|
|
I don't like die
/me too. In fact my programs are perfect and never die. Being such a perfect programmer, not only the OS and environment and filesystem they run on are also perfect and never cause failures, etc. but even the users of my programs are perfect (the most prominent one being myself - so that's already a guaranteee!) and never make errors like supplying inexistent files, etc.
Seriously, web development is not the whole world. die is a perfectly valid and handy language feature - indeed for complex applications, possibly running a GUI, it is sensible to adopt other ways round other than dieing, like warning the user and asking for more input. But for a script that expects a filename on the cmd line to parse, I can't see a more intelligent option than to die, if the file does not exist, for example.
| [reply] |
|
|