Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am building a system that has hundreds of pages and subroutines that we call into the main .cgi file. I know that using my in a subroutine makes that variable only usable in that subroutine.

I need to have certain variables work in every subroutine and every page. In the main CGI file, I pull a hashref out of a database like this:

$sth = $dbh->prepare(q{SELECT * FROM `sometable` WHERE `tid` = ?}); $sth->execute($_memberId); $_mem = $sth->fetchrow_hashref(); $sth->finish();

I need $_mem to work on every page and in every subroutine. It is not working, I am having to rerun that pull from the database 2 or 3 times within the same loading of all the files.

So how can I make certain variables, such as $_mem (hashref pulled like $_mem->{username}) and others that are just regular variables?

thank you!

Junior

Replies are listed 'Best First'.
Re: Site Wide Variables
by jdporter (Paladin) on Aug 04, 2007 at 05:38 UTC

    use vars. Ignore the bit about it being obsolete; it isn't.

      I use that at the top of the cgi file:
      use vars qw($_mem $cookie $sess_id $sess_ref $logged_in $_pgtitle $_title %in $_un $_scripid $_req_dir $_pg $_page_content $_pageName $_surl %vars $_show_message $dbh $_dur_time $debug $inc_sess_id);
      The code $_mem is populated in the .cgi file, but below that code I pull in another file:
      require "/home/username/req/vars.conf";
      And I use $_mem in there and in other files I require into the document, but they often appear empty, when I do it this way:
      if($_mem) { # Do something here } else { # Nope it is empty }
      And it always runs the else statement because it shows empty in the .conf file, but in the .cgi file it is populated.

      Am I doing something wrong?
      Does calling it vars.conf and not putting a perl interpreter call into it, make a difference? (In other words would it work better if I made it vars.cgi and put the #! shebang line in it?)

      thank you
      Junior
        Does calling it vars.conf and not putting a perl interpreter call into it, make a difference? (In other words would it work better if I made it vars.cgi and put the #! shebang line in it?)

        No no, you definitely want to require it. As a require'd file, the shebang line and filename "extension" are not special.

        As for what's really going wrong, it's hard to say without seeing more of your code.

        A word spoken in Mind will reach its own level, in the objective world, by its own weight
Re: Site Wide Variables
by GrandFather (Saint) on Aug 04, 2007 at 08:45 UTC

    It may be that what you are looking for is to wrap up $_mem (and perhaps $dbh) in a singleton object derived from Class::Singleton. That way you don't need to worry about where the first use of $_mem or $dbh is made or in what order. Whichever is used first, the singleton code will ensure that they are initialized correctly and in the right order.


    DWIM is Perl's answer to Gödel
Re: Site Wide Variables
by akho (Hermit) on Aug 04, 2007 at 07:28 UTC
    I'd make a package with a package variable containing $_mem (then initialize it once and import everywhere).

    You may be able to adapt code from Application-wide configuration, actually.

      how do you import a variable in other areas?

      I think I see one of my problems...

      At the top, my actual $dbh is set in the vars.conf file, and so before I can verify a member I have to call that vars.conf file, yet in that vars.conf file I execute a lot of things, including using some of the $_mem variables, although they are in sub routines, that I don't call until after I set $_mem does that make a difference?

      thank you.

      Junior
        You declare it as our in the package, then use the package elsewhere and use a fully qualified name for the variable (like $My::Package::var).

        Ditn't understand the third paragraph.