That's the convention in perl. One way to use it is like this,
A blessed reference does not evaluate false, so defined is not needed in the logic.my $foo = MaybeADirectoryObject->new(@args) or die 'No object', $!; # ...
The eval {die 'No Object' if $error} with $@ handling is really no cleaner. In fact, eval returns undef if block exit is by die-ing or other trappable error, and can be handled the same as the more direct code above.
Update: I previously had the constructor wrapped in defined, but changed it and the description just as ++ctilmes posted his useful comment.
The big difference between perl and C++ exception handling is that, in C++, catch clauses are the responsibility of the try block, while in perl they are the responsibility of the caller. That is a large conceptual gap, but is not so different in application.
After Compline,
Zaxo
In reply to Re: Should a constructor ever return undef?
by Zaxo
in thread Should a constructor ever return undef?
by tall_man
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |