I have a config file (config.pl) with my constants :
#!/usr/bin/perl
use strict;
use warnings;
use Net::Domain qw(hostname hostfqdn hostdomain domainname);
use constant URL => "http://".domainname()."/";
use constant CGIBIN => URL."cgi-bin/";
use constant CSS => URL."html/css/";
use constant RESSOURCES => URL."html/ressources/";
...
And I would like to use these constants in index.pl, so index.pl starts with :
#!/usr/bin/perl -w
use strict;
use CGI;
require "config.pl";
how to use URL, CGI... in index.pl ?
Thanks,
Bye
EDIT
I found a solution :
config.pm
#!/usr/bin/perl
package Config;
use strict;
use warnings;
use Net::Domain qw(hostname hostfqdn hostdomain domainname);
use constant URL => "http://".domainname()."/";
use constant CGIBIN => URL."cgi-bin/";
1;
index.pl
BEGIN {
require "config.pm";
}
print Config::URL;
End
Constants may be lists of more (or less) than one value. A constant with no values evaluates to undef in scalar context. Note that constants with more than one value do not return their last value in scalar context as one might expect. They currently return the number of values, but this may change in the future.
You use the Const statement to declare a constant and set its value. By declaring a constant, you assign a meaningful name to a value. Once a constant is declared, it cannot be modified or assigned a new value. You declare a constant within a procedure or in the declarations section of a module, class, or structure.
In Perl the our keyword is used to declare one or more package variables. More exactly it creates lexical alias to a packaga variable, but for our practical purposes our means we are allowed to use the package variable without giving the fully qualified name and without violating the rules of use strict.
A require statement is evaluated at execution time. If the VERSION argument is present between Module and LIST, then the use will call the VERSION method in class Module with the given version as an argument. The default VERSION method, inherited from the UNIVERSAL class.
What you want to do here is setup a Perl module that you can export from.
Place the following into 'MyConfig.pm':
#!/usr/bin/perl
package MyConfig;
use strict;
use warnings;
use Net::Domain qw(hostname hostfqdn hostdomain domainname);
use constant URL => "http://".domainname()."/";
use constant CGIBIN => URL."cgi-bin/";
use constant CSS => URL."html/css/";
use constant RESSOURCES => URL."html/ressources/";
require Exporter;
our @ISA = 'Exporter';
our @EXPORT = qw(hostname hostfqdn hostdomain domainname URL CGIBIN CSS RESSOURCES);
And then to use it:
use MyConfig; # which means BEGIN {require 'MyConfig.pm'; MyConfig->import}
By setting @ISA
to Exporter
in the MyConfig
package, you setup the package to inherit from Exporter
. Exporter
provides the import
method which is implicitly called by the use MyConfig;
line. The variable @EXPORT
contains the list of names that Exporter
should import by default. There are many other options available in Perl's documentation and in the documentation for Exporter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With