tall_man has asked for the wisdom of the Perl Monks concerning the following question:
As a matter of object-oriented style, what do you think of a class constructor like this:
Another programmer in my shop did something like this, and a third programmer was caught by it when they set the return value of new() into a variable and called a method on it much later in the code.package MaybeADirectoryObject; use strict; sub new { my ($class, $dir) = @_; if(! -e $dir) { print "The directory $dir does not exist\n"; return; } my $self = {}; # Initialize $self using directory here... bless($self, $class); return $self; }
Since I started object-oriented programming with C++, I find it very strange to return an undef from a constructor. I could see calling die and letting the caller handle the exception with an eval block, but returning an undef seems wrong to me.
What do you think?
Back to
Seekers of Perl Wisdom