http://www.perlmonks.org?node_id=301133

jest has asked for the wisdom of the Perl Monks concerning the following question:

I'm curious about a technique I've been using for a few months (successfully), which nonetheless seems as if it might not be quite kosher. Basically, in the middle of a constructor, I leave it to run a different program, never to return. This is regular and expected behavior, not an exception.

A simplified version of what I'm trying to do looks more or less like this:

sub new { my ($class, %arg) = @_; my $self = { table => $arg{table} || 'test', message => $arg{message}, dbh => MyDatabase::DBManager::connect(), cookie_name => $cookie_name, # other similar stuff here }; _login_user($self); return bless $self, $class; } sub _login_user { my $self = shift; # do something to log the user in based on $self->{cookie_name}, # returning $sess_ref as a reference to a db-stored session; then: if (!defined($sess_ref)) { print CGI::redirect(-uri=>'login.cgi'); } # i.e. if there's no session, bounce the user straight to a # login page, thus leaving in the middle of the constructor, # before hitting the "bless" else { # $sess_ref is defined, store some session info into # $self and get on with life, returning to "new" and to the # original program that called this module } }

It's been working fine, but I wonder if there's something wrong with doing this as a regular procedure; yet I didn't find anything specifically about it in Camel, Conway, the perltoot & related docs I checked, etc.

So is this safe, or not? If not, what's a good solution, given that the whole point of modularizing this login process is to keep identical code all together?