*I* *usually* have an init() method called automatically in new() to do any initialisation, e.g. connect and read DB. This has the advantage of being able to re-initialise any time I like after instantiation by just calling $obj->init().
I am still undecided (or haven't read enough) on how to propagate errors, error messages and error codes to caller all the way up. The method you are using by saving the *last* error message will need some tweaking when you are nesting sub calls (and if you do that it will soon result to a paper-pushing system). Perhaps simply append to $error with obligatory clearing it when starting a-fresh. Alternatively throw an exception which is the "modern" thing to do but that imposes a whole new style which I don't see often in Perl modules (arbitrary judgement), see e.g. Exception::Class. Sometimes I return a complex data structure with error code, error message, data etc. but I only do that for complex subs.
I appreciate Perl more when I have to work with Java and its super-strict typing.:
sub work {
my $something_wrong = 1;
if( $something_wrong ){
#return Error->new(0, "because ...");
return bless [0, "because ..."] => 'Error'
}
# edit: changed to a hashref to show returning different types
# it need not be a ref at all, e.g. can send a hash too
#return [1,2,3]
return {1=>2, 3=>4}
}
my $ret = work();
if( ref($ret) eq 'Error' ){ die "error: @$ret" }
print "got some results:".Dumper($ret);