in reply to Re: Our Across Multiple Files?
in thread Our Across Multiple Files?

I have a single DBH connection object, a single instance object, and others for a one-time project I'm working on myself. I know its not exaclty great technique, but because I'm the only one that will ever see it, I don't think it will hurt in the long run.

Replies are listed 'Best First'.
Re: Re: Re: Our Across Multiple Files?
by djantzen (Priest) on Dec 18, 2001 at 08:18 UTC

    You could just put it in package main, as in $main::db = new DB();, although it's one of those design choices that can get very deeply sedimented in a large project, and hard to change later.

    If you're feeling amibitious, why not try a singleton class, with a class method that returns the data you need?

    package GlobalVarsSingleton; my $instance; # a 'private' ctor sub _new { # init stuff } sub Instance { my ($class, %args) = @_; unless (defined $instance) { $instance = $class->_new(%args); return $instance; } else { return $instance; } }

    The singleton ensures that you'll only have one instance of the data per interpreter, and the calling code doesn't have to care where that data lives.

Re: Re: Re: Our Across Multiple Files?
by mr_mischief (Monsignor) on Dec 18, 2001 at 00:57 UTC
    The only remaining caveat is that code not meant to stick around sometimes does. I have code written as throw-away from year ago slightly tweaked and running in production on servers today. Oh, and that Unix thing so many people (including myself) love? The original was meant to be an in-house project...