Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • I don't know why you have the $b in there. Does every parent object wrap a child object?

    If you need a child object to create a parent, and can use a singleton, just lazily initialize it. The first pass will loop around a bit, but every pass after that, you're already done.

    --
    • Randal L. Schwartz
    • Stonehenge
    • by jjohn (22) on 2002.11.08 0:57 (#14607) Homepage Journal

      I don't know why you have the $b in there. Does every parent object wrap a child object?

      The parent merely needs to use (and then discard) the child object. To make things a little more concrete, the Parent class (not it's real name) is there to provide both a reasonable object constructor for children and AUTOLOAD object properties accessor methods. For public methods, this class has a series of methods that return DBI handles, so that DB credentials are centralized in one place.

      Child classes provide convenient objects for complex data. For instance there is a child class called User that saves and stores data about a user to a mysql database. Because this is a web app, the User class is able to read an HTTP cookie (created with Apache::AuthCookie) that has the current user's credentials (ok, the cookie has a session ID which can be used to get at this info [Thanks Apache::Session!]).

      Now why would I want an instance of User in this generic Parent class? Because sometimes I need to return different DBI handles based on the kind of User. The Parent has a method called companies_dbh() which returns a DBI handle to main database (whatever that means). Certain users will be looking at completely distinct data kept on a separate machine. Of course the data has the same structure as the "regular" data.

      By using the User class, the Parent can smartly and magically deliver the correct DBI handle to existing scripts.

      If this rambling explanation made any sense, you'll see that this is a fairly unique situation. I thought I'd post the code (strip of all it's complex context) because the essence of the bug is fairly simple. And yet it took me several minutes to locate the problem.

      • Ahh... I get it. You've got the wrong model. If

        Child->new

        might not return an object of type Child, then you're asking the wrong class. You just need a

        Parent->object_for_situation(@this)

        method, which has a lazy initialization for creating a specific Parent handle to help decide which Child_nnn class to create.

        There. That sorts it out. I'd be very confused if calling

        Child->new

        returned a Child2 instead of a Child. It doesn't make sense from an OO perspective.

        --
        • Randal L. Schwartz
        • Stonehenge