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?


In reply to OO: Leaving a constructor midway? by jest

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.