Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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,
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/";
index.pl
BEGIN {
require "config.pm";
print Config::URL;
–
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
–
–
as the last statement in the file. And print returns a true anytime it prints a single character.
See require.
Troubleshooting
It could be a directory issue. The .pl file must be in @INC or modified by a path to the file.
Try this:
perl -Mconfig.pl -e 1
If it fails, look at the error message. Actually, in any event, you should be getting more with strict and warnings than "Oops, it failed."
You can't use barewords when you use strict.
A bareword is a essentially a variable name, without a sigil ($,@,%,&,*).
I suggest taking a look at the Readonly module on CPAN for creating constants.
The use constant pragma has a bunch of arcane magic going on that can result in difficult to debug code, if you're not careful.
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.