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; }

In reply to Re: OOP Access of variables by TheoPetersen
in thread OOP Access of variables by simeon2000

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.