Global Defines 
Author Message
 Global Defines

Greets,

I am wondering what is the best way to set up global defines in Perl.  I
would like to have a centralized configuration file that contains all of my
"user settable" variables, so that I don't have to hard code them in the
scripts.  I am using Perl object classes, so I'm not sure what the best way
to do this is.

In PHP, another language I am working with, I created a centralized DEFINES
file, and using the PHP 'define' function, was able to set my script
variables to the values in this centralized file.  For example:
define("LDAP_SERVER_IP", "192.168.0.1");  I can now make references to
$LDAP_SERVER_IP.

What is the best way to do this in Perl, IYO?

Cheers,
Jason



Sun, 24 Aug 2003 23:45:58 GMT  
 Global Defines

Quote:

> define("LDAP_SERVER_IP", "192.168.0.1");  

> What is the best way to do this in Perl, IYO?

Would you have a problem with
        $LDAP_SERVER_IP = "192.168.0.1";
???

--
John Porter



Mon, 25 Aug 2003 06:08:21 GMT  
 Global Defines
: Greets,

: I am wondering what is the best way to set up global defines in Perl.  I
: would like to have a centralized configuration file that contains all of my
: "user settable" variables, so that I don't have to hard code them in the
: scripts.  I am using Perl object classes, so I'm not sure what the best way
: to do this is.

: In PHP, another language I am working with, I created a centralized DEFINES
: file, and using the PHP 'define' function, was able to set my script
: variables to the values in this centralized file.  For example:
: define("LDAP_SERVER_IP", "192.168.0.1");  I can now make references to
: $LDAP_SERVER_IP.

: What is the best way to do this in Perl, IYO?

The following is not in the least object orientated, but...

        #!perl
        # typical script
        BEGIN { require '/path/to/your/config-file.pm' }
        use strict;

        #!perl
        # config-file.pm
        $IMPORTANT_GLOBAL_VARIABLE = 'its value';

I use 'require' because you can specify the path of the file.  The config
file can then set the INC path for including other modules.  BEGIN is used
so it happens before anything else.

The path to the config file can also be relative,
        require './my-config.pm';
        require '../lib/my-config.pm';
        ..etc..

which is more flexible as long as you trust the current directory in which
the script will run.

Require also accepts a variable as the config file name, which allows
lines such as

e.g.    BEGIN{ require ($ENV{'CONFIG'}||'./config.pm') }

which is very flexible for testing, though doesn't pass a taint test.
(The following does pass the taint test, though the m// should be
improved)
        BEGIN{ require (($ENV{'CONFIG'}=~m/(.*)/,$1)||'./config.pm') }

'use strict' and 'use vars' allow you to check for typos on the global
variable names, which is very useful.  Put the names of your global
variables into one or more arrays, and then pass the array of names to
'use vars'.  In this case notice what is being passed to 'use vars'





Mon, 25 Aug 2003 06:41:39 GMT  
 Global Defines

Quote:


> : Greets,

> : I am wondering what is the best way to set up global defines in Perl.  I
> : would like to have a centralized configuration file that contains all of my
> : "user settable" variables, so that I don't have to hard code them in the
> : scripts.  I am using Perl object classes, so I'm not sure what the best way
> : to do this is.

> : In PHP, another language I am working with, I created a centralized DEFINES
> : file, and using the PHP 'define' function, was able to set my script
> : variables to the values in this centralized file.  For example:
> : define("LDAP_SERVER_IP", "192.168.0.1");  I can now make references to
> : $LDAP_SERVER_IP.

> : What is the best way to do this in Perl, IYO?

> The following is not in the least object orientated, but...

>    #!perl
>    # typical script
>    BEGIN { require '/path/to/your/config-file.pm' }
>    use strict;

>    #!perl
>    # config-file.pm
>    $IMPORTANT_GLOBAL_VARIABLE = 'its value';


Or to be slightly more modern:

        #!perl
        # typical script
        use MyConfig;
        use strict;
        warn $foo;
        warn $bar;

        #!perl
        # config file
        package MyConfig;
        use base 'Exporter';

        $foo = "foo";
        $bar = "bar";

Quote:

> I use 'require' because you can specify the path of the file.  The config
> file can then set the INC path for including other modules.  BEGIN is used
> so it happens before anything else.

> The path to the config file can also be relative,
>    require './my-config.pm';
>    require '../lib/my-config.pm';
>    ..etc..

> which is more flexible as long as you trust the current directory in which
> the script will run.

If you cannot, then use FindBin:

        use FindBin;
        require "$FindBin::RealBin/my-config.pm";

Regards,
        Slaven

--
use Tk;$c=tkinit->Canvas(-he,20)->grid;$x=5;map{s!\n!!g;map{create$c 'line'=>
map{$a=-43+ord;($x+($a>>3)*2=>5+($a&7)*2)}split''}split"!";$x+=12}split/_/=>'K
PI1_+09IPK_K;-OA1_+K!;A__1;Q!7G_1+QK_3CLPI90,_+K!;A_+1!KQ!.N_K+1Q!.F_1+KN.Q__1+
KN._K+1Q!.F_1+KN.Q_+1Q__+1!KQ!.N_1;Q!7G_K3,09Q_+1!K.Q_K+1Q!.F_1+KN.Q_';MainLoop



Mon, 25 Aug 2003 09:10:15 GMT  
 Global Defines

Quote:

> Greets,

> I am wondering what is the best way to set up global defines in Perl.  I
> would like to have a centralized configuration file that contains all of my
> "user settable" variables, so that I don't have to hard code them in the
> scripts.  I am using Perl object classes, so I'm not sure what the best way
> to do this is.

> In PHP, another language I am working with, I created a centralized DEFINES
> file, and using the PHP 'define' function, was able to set my script
> variables to the values in this centralized file.  For example:
> define("LDAP_SERVER_IP", "192.168.0.1");  I can now make references to
> $LDAP_SERVER_IP.

> What is the best way to do this in Perl, IYO?

> Cheers,
> Jason

You can set up a file that contains simple variable declarations:
$value1 = value1
...
$valueX = valueX

Then you can 'require' this file in all your perl scripts close to the
top:
require('/path/to/file');
for more info:
perldoc perlfunc

If you want the files to be constants:
use constant VALUE1 => 1;
for more info:
perldoc constant

-> W

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Wesley Wannemacher
Instructor / Network Administrator
University of Northwestern Ohio
http://www.unoh.edu/

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--
: 1.  What is the possibility of this being added in the future?
In the near future, the probability is close to zero.  In the distant
future, I'll be dead, and posterity can do whatever they like...  :-) --lwall
--



Mon, 25 Aug 2003 05:53:38 GMT  
 Global Defines
I'm not arguing here, just expanding on the reasons for the choices I made
in the examples I included.



: > : Greets,
: >
: > : I am wondering what is the best way to set up global defines in Perl.  I
....snip...
: >  #!perl
: >  # typical script
: >  BEGIN { require '/path/to/your/config-file.pm' }
....snip...

: Or to be slightly more modern:

:       #!perl
:       # typical script
:       use MyConfig;


perl.  In different environments it will be different, and perhaps not
even include the directory containing your files (such as '.').  Also, if
anyone else puts a file called 'MyConfig.pm' in any directory ahead of
yours then your script breaks.  There may not be a good reason for someone
to do this, but that doesn't it may never happens.

The point is that your script cannot depend upon where it will find its
modules until AFTER is has loaded that first configuration module - a
classic chicken and egg problem.

I suggested 'require', not 'use', specifically so that the calling script
can control the path used to find its initial configuration module.

: > so it happens before anything else.
: >
: > The path to the config file can also be relative,
: >  require './my-config.pm';
: >  require '../lib/my-config.pm';
: >  ..etc..
: >
: > which is more flexible as long as you trust the current directory in which
: > the script will run.

: If you cannot, then use FindBin:

:       use FindBin;
:       require "$FindBin::RealBin/my-config.pm";

Just to point out, Findbin finds the directory where the script is
installed, which is not the same thing as the current directory.

What the example above tries to show is that if you can depend upon being
in a specific directory then you can also depend upon reading a specific
config file.  "depending" upon being in a specific directory is not the
same thing as controlling which directory that is.

For example, for a particular web server, you may be able to depend upon
which directory a script is run in.  Add some links to the original script
and the script can now run in any of several directories.  You don't know
in which directory the script will run, but you can trust the relative
path to get the correct config file for each use of the script.



Tue, 26 Aug 2003 02:42:48 GMT  
 Global Defines

Quote:

> I'm not arguing here, just expanding on the reasons for the choices I made
> in the examples I included.



> : > : Greets,
> : >
> : > : I am wondering what is the best way to set up global defines in Perl.  I
> ....snip...
> : >     #!perl
> : >     # typical script
> : >     BEGIN { require '/path/to/your/config-file.pm' }
> ....snip...
> : Or to be slightly more modern:
> :          #!perl
> :          # typical script
> :  use MyConfig;

[snip]

Quote:
> The point is that your script cannot depend upon where it will find its
> modules until AFTER is has loaded that first configuration module - a
> classic chicken and egg problem.
> I suggested 'require', not 'use', specifically so that the calling script
> can control the path used to find its initial configuration module.


the beginning (like unshift would)? That should be enough to get
you to the proper config file, even if it is a .pm-style module.
Best of both worlds.

###----- _begin code_ -----
BEGIN{
    print "\n";

        print $_ . "\n"
    }

Quote:
}

use lib q{.};
print "\n";

    print $_ .  "\n";
Quote:
}

print "\n";
###----- _end code_ -----

###----- _begin output_ -----
/usr/lib/perl5/5.00503/i386-linux
/usr/lib/perl5/5.00503
/usr/lib/perl5/site_perl/5.005/i386-linux
/usr/lib/perl5/site_perl/5.005
.

.
/usr/lib/perl5/5.00503/i386-linux
/usr/lib/perl5/5.00503
/usr/lib/perl5/site_perl/5.005/i386-linux
/usr/lib/perl5/site_perl/5.005
.

###----- _end output_ -----

Chris

--
Christopher E. Stith
Where there's a will, there's a lawyer.



Tue, 26 Aug 2003 08:15:56 GMT  
 Global Defines

Quote:
> I am wondering what is the best way to set up global defines in Perl.  I
> would like to have a centralized configuration file that contains all of
my
> "user settable" variables, so that I don't have to hard code them in the
> scripts.  I am using Perl object classes, so I'm not sure what the best
way
> to do this is.

Yet another way to do it:

# config file
package Config;
$config{x} = 5;
$config{y} = 4;

# script:
package Script;
do '/usr/local/config.pl';
print $Config::config{x};
print $Config::config{y};

do() has the minor advantages of being the only way that works in a mod_perl
enviornment, and the fact that it doesn't raise an exception if the config
file is f*ed (uses the return value).

Quote:
>I am using Perl object classes, so I'm not sure what the best way
> to do this is.

    The true OO way to do this would be to make a separate configuration
file for each object (those vars that each object needs), then make a
configuration program that allows the user to change any config variable on
one screen and automagically makes the change to the proper config file.
Each object reads its own configuration file in its constructor.  This also
has the huge advantage of not allowing the user to mess up the config file.
On the other hand, reading hundreds of config files is not very practical.

    Failing that, you could make a Configuration object that is a singleton,
and use/contain it in each of your other objects:

# Config object
package ConfigObject;
$is_instantiated = 0;
$instance;

sub new {
    my $class = shift;

    if ( $is_instantiated ) {
        return $instance;
    } else {
        do '/usr/local/config.pl';
        $instance = \%Config::config;

        bless $instance, $class;
        return $instance;
    }

Quote:
}

sub get_time {
    my $self = shift;

    return $self->{time};

Quote:
}

# Other problem domain object
package RandomObject;
use ConfigObject;

sub new {
    my $class = shift;

    my $self = {};
    $self->{config} = Config->new();

    bless $self, $class;
    return $self;

Quote:
}

sub print_time {
    my $self = shift;

    print $self->{config}->get_time();

Quote:
}

As you can see, this is a little clumsy.  Anything that spans multiple
objects usually can't be represented well in OO.

                 -Mike



Fri, 29 Aug 2003 08:19:42 GMT  
 Global Defines

Quote:



> > I am wondering what is the best way to set up global defines in Perl....

There are also several modules on CPAN that handle application
configuration.  I've used App::Config with success - it's very
flexible, and reads ASCII configuration files.  Values can also
be changed by method calls at runtime, if you want.

Quote:
>     The true OO way to do this would be to make a separate configuration
> file for each object ....

>     Failing that, you could make a Configuration object that is a singleton,
> and use/contain it in each of your other objects

> # Config object
> package ConfigObject;
> ...
> As you can see, this is a little clumsy.  Anything that spans multiple
> objects usually can't be represented well in OO.

I've used this second method, the "configuration object."  It may be a
little
clumsy, but it makes more sense if what you are configuring is the
application
itself, which happens to be built of objects that the user need not
really
know about.  Also, App::Config supports configuration file "blocks" so
you can
arrange parameters in logical units, and each object's constructor can
use
the relevant block.


Sat, 30 Aug 2003 21:30:05 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. How to save an image in a database

2. Wich port adres is the LPT1 on?

3. Differences between BP and TP (was: comp.lang.pascal.borland FAQ)

4. defining global variables with an installer script

5. How can i define a global variable ?

6. Defining Global Script Variables

7. Defining global variables with an installer script

8. define global variables

9. Defining a global hash ref ...

10. defined keys that aren't defined

11. PERLFUNC: defined - test whether a value, variable, or function is defined

12. PERLFUNC: defined - test whether a value, variable, or function is defined

 

 
Powered by phpBB® Forum Software