Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Just another Perl shrine
 
PerlMonks  

Re: OOP Access of variables

by TheoPetersen (Priest)
on Mar 08, 2001 at 16:27 UTC ( [id://63020]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to OOP Access of variables

The usual way would be to store the dbh in a package variable and have a method for it, i.e.
package TArtLib; use strict; use vars qw($dbh); ... sub dbh { return $dbh; }
Then TArt::Editor invokes the method:
package TArt::Editor; use TArtLib; ... my $dbh = TArtLib->dbh;
Note the usage of TArtLib inside of TArt::Editor; that relieves the code which calls TArt::Editor from knowing what other modules are required.

The dbh class method would also be a good place to initialize things (or call an initialization method) if $dbh is false.

Update:If what you want is one-time initialization, then this is definitely the way to go. Don't use the package $dbh directly, but go through a method as shown here; and use that method to do the one-time initialization:

sub dbh { unless ($dbh) { # connect to the database now. } return $dbh; }

Replies are listed 'Best First'.
Re: Re: OOP Access of variables
by simeon2000 (Monk) on Mar 08, 2001 at 16:35 UTC
    Theo: Thanks, that's a good idea. I just have another question:

    If I also have a TArt::Article package data object, and I call the script like this:

    package main; use TArt::Editor; use TArt::Article; my $ed = new TArt::Editor; my $art = new TArt::Article;
    If both of these packages (TArt::Editor, TArt::Article) use TArtLib, would they both get the same $dbh object when calling TArtLib->dbh, or would it be two separate TArtLib packages (and thus, two database handle objects)?
      Assuming the code I gave you, there would be only one $dbh. (Hey, that's a catchy phrase ... :)

      When learning Perl objects (or Perl anything else), follow your hunches and try them out. You'll develop both knowledge and intuition quickly. That's the nice thing about Perl's learning curve, you can almost always do useful work along the way.

        Theo: You were right! I forgot that Packages and the object they create are separate. I figured for each Article or Editor I created, there'd be a new $dbh handle, but since it's global in TArtLib, the ref on the $dbh returned the same memory address for both packages' copy of $TArtLib::dbh :) Thanks so much to everyone for help. I learned a lot more about packages and objects today :)
        I'd just add in here for clarification of the SoPW poster that $dbh is a reference to your database object (the arrow operator for the DBI functions should be a big tip-off :D); using the "my $dbh = TArtLib->dbh();" anywhere else gains you a local copy fo the reference, but the reference still points to the single copy of the object that holds the DB connection.


        Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://63020]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.