SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Nov 2002
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Files and Variables

    I am having a little problem. I would like to know if there is a way to do the following?

    File 1.
    ....require File2;
    ....require File3;

    ....ts_db_connect();
    ....ts_do_something();
    ....ts_db_disconnect();

    File 2.
    ....our ($dbh, $sth);

    ....sub ts_db_connect {
    ........#...Connect
    ........return $dbh;
    ....}

    ....ts_db_query {
    ........$sth = $dbh->prepare($Query);
    ........$sth->execute;
    ........return $sth;
    ....}

    ....sub ts_db_disconnect {
    ........#...Disconnect
    ....}

    File 3.
    ....sub ts_do_something {
    ........ts_db_query(...);
    ........#...Doing Something
    ........$sth->finish;
    ....}

    As you can see what I want to know is how do I make it so that $sth is accessible across all files (without having to do use vars qw// at the top of every file, or something similar).

    Also, I was wondering if anyone could help me with this:

    File 1.
    ....use configuration;
    ....use File2;

    ....print MY_CONSTANT;
    ....print_it();

    configuration.pm
    ....BEGIN {
    ........use Exporter();

    ........our @ISA = qw(Exporter);
    ........our @EXPORT = qw(MY_CONSTANT);
    ....}

    ....use constant MY_CONSTANT => 'value';

    ....END { }

    File 2.
    ....sub print_it {
    ........print My_CONSTANT;
    ....}

    Just like above, I would like to know is there an easy way to have MY_CONSTANT accessible from every file that the program uses. If there is not, and I decided to add 'use configuration' in every file would that really be like using X many more files or would it just open up the name space. Phrased Differently: If I were to place 'use configuration' at the top of every file would it require more RAM?

    Thanks for any help you can give me.

  2. #2
    SitePoint Enthusiast eych's Avatar
    Join Date
    Nov 2002
    Location
    Moscow, Russia
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Greetings,

    You can always call a variable with a fully qualified package name.

    For example, you can have the $sth variable defined in your main script and then call it from any other package that you "require" like this:

    Code:
    $main::sth
    Or, alternatively, you can use the same package in all required files. If you will use the same package then all the files will share the same name space and thus a variable defined in one of the files will be also accessible in other files from the same package.

    You question about importing configuration variable is basically the same. Perl compiles one file only once, the next time you try to "require" it or "use" it will just "open the name space" like you have written.

    Hope that will help.
    Last edited by eych; Dec 1, 2002 at 02:58.
    Sergey Smirnov
    One of the Stuffed Guys
    http://www.stuffedguys.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •