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

Hi,
I am using some constants in my script to connect my database. somethink like that:
use constant LOGIN => 'name'; use constant PASS => 'pwd'; use constant IP => '190.xxx.xxx.xx'; use constant DB_NAME => 'mydbname';
Now, I would like to transfer these constants in another file (.pm) and reuse them.
I did
package constant; use Exporter (); @ISA = qw (Exporter); @EXPORT_OK = qw ( LOGIN PASS IP DB_NAME); use constant LOGIN => 'name'; use constant PASS => 'pwd'; use constant IP => '190.xxx.xxx.xx'; use constant DB_NAME => 'mydbname'; 1;
then, in my script I tryied
use constant; @ISA = qw (constant); ... DBI->connect ("DBI:mysql:host=".IP.";database=".DB_NAME."", LOGIN, PAS +S,{PrintError => 0, RaiseError => 1})
but it doesn't work. constants are not recognized. I am not very familiar with inheritance.
could you help me? where am i wrong ? maybe is there a simpler way to do that...

Thanks in advance

Replies are listed 'Best First'.
Re: inheritance of constants
by BrowserUk (Patriarch) on Nov 24, 2004 at 10:21 UTC

    If you put the constants/subs to be exported into @EXPORT_OK, they will only be exported to the caller's namespace if the caller requests them on the use line:

    use MyConstants qw/LOGIN PASS IP DB_NAME/; ...

    Note: I've renamed your package to MyConstants. Two reasons:

    1. All lowercase package names are notionally reserved for pragmas (system defined packages).
    2. constant is already used for the pragma you are using to define your constants.

    If you are sure that you always want to export those constants, then you can alternatively add them to @EXPORT, and they will exported into every namespace that uses the module. This is vagely frowned upon as it doesn't give any clue as to where they came from in the calling code, but in the case of constants and a package called MyConstants or similar, it would probably be acceptable to most people. Expecially if there are a lot of constants that will usually need to be imported.

    Update: Corrected s/@EXPORTS/@EXPORT/ courtesy of ysth.


    Examine what is said, not who speaks.
    "But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
    "Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: inheritance of constants
by borisz (Canon) on Nov 24, 2004 at 10:19 UTC
    your constant module should have another name, constant is already in use!
    package MyConstants; require Exporter; @ISA = qw (Exporter); @EXPORT_OK = qw ( LOGIN PASS IP DB_NAME); use constant LOGIN => 'name'; use constant PASS => 'pwd'; use constant IP => '190.xxx.xxx.xx'; use constant DB_NAME => 'mydbname'; 1;
    and your code should only import the constants. No need for inherence.
    use MyConstants qw/LOGIN PASS IP DB_NAME/; DBI->connect ("DBI:mysql:host=".IP.";database=".DB_NAME."", LOGIN, PAS +S,{PrintError => 0, RaiseError => 1})
    Boris
Why are you sharing constants in a module?
by rrwo (Friar) on Nov 24, 2004 at 11:35 UTC

    This might be completely off the mark, but why are you putting constants in a module instead of a configuration file?

    If it's to "hide" login information so that it's not in a script where people may be able to see it, that may not be a good idea since anyone who can access the module in a script with a 'use' call can access that information.

    In some cases it makes more sense to put that information in a separate configuration file that you can restrict access to certain processes.

    Other advantages of using a configuration file as opposed to a module:

    • A sysadmin can change a configuration file without touching code and accidentally causing an error.
    • The same code can be used for multiple databases by changing a reference to the configuration file rather than creating another module.
Re: inheritance of constants
by fglock (Vicar) on Nov 24, 2004 at 13:33 UTC

    Constants are inherited, actually - but you have to use method calls:

    use strict; package Const; use constant LOGIN => 'user'; package Pac; use vars '@ISA'; push @ISA, 'Const'; package Main; print Pac->LOGIN, "\n"; # user
Re: inheritance of constants
by mat21 (Beadle) on Nov 24, 2004 at 13:30 UTC
    >borisz and browserUk I applied your recommendations and it works!
    Thank you so much for your useful explanations.

    >rrwo: Actually, as I mentionned, I had these constants in the main script. then, in order to improve the "readability" of my files, I thought to split into 2 files. My first idea (the simplest; few changes to do) was to create a new module but a configuration file could be convenient as well.
    How would I proceed? open(.txt) and regular expression to retrieve my info ?
      Take a look at the modules in the Config name space for tools to assist in easily creating/manipulating configuration files.

      ----
      I Go Back to Sleep, Now.

      OGB