in reply to Re: OO: Leaving a constructor midway?
in thread OO: Leaving a constructor midway?

Why not do this:
my $book = MyPackage->startup( $stuff ); package MyPackage; sub startup { # do stuff here, like creating the object # do your check for authority here and exit, if appropriate # Do more stuff here return $book; }

This is the method used by classes like DBI. Calling it startup() makes it more clear to your maintainer (which may be you!) what exactly you're doing. (Which is why I suspect Tim Bunce chose connect() instead of new() for DBI.)

------
We are the carpenters and bricklayers of the Information Age.

The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re: Re: Re: OO: Leaving a constructor midway?
by jest (Pilgrim) on Oct 22, 2003 at 19:59 UTC

    Silly question, but: what's the difference? I mean, I see that there's a difference behind the scenes, in that this suggestion involves calling a separate constructor that really does bless the object, and then does the login business etc. But from the point of view of the user, it's the same: you call some method (whether called "new" or "startup"), and either bounce to a different program, or get back an object blessed into MyPackage. After all, you are the one who so recently said to Abigail-II that if I call a method and get back something that quacks like an object, it's a constructor, so....

    If this version makes the OO gods smile upon me, I'm happy to go with it, but I'm just curious. Several people seemed to react with horror to the idea of a constructor exiting the program, so IDG why it's not a horror to having a constructor-like thing exiting the program.

      The difference here is that startup() wraps the constructor. That constructor is still (possibly) available to the client. This is an important distinction. This is (kinda) how DBI does it. Now, DBI's constructor(s) are not visible from the outer-most client, but it is used by the DBD:: class that DBI is providing a connection through.

      Now, DBI is a pathological example, cause it does so much more than just provide an object. But, I think you get my point.

      Also - who cares if the "OO gods" smile on you. It's your code, you have to maintain it, and you're the one who has to look at yourself in the mirror. If you're happy with it, the the heck with everyone else.

      ------
      We are the carpenters and bricklayers of the Information Age.

      The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

      ... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.