添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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;
                You're heading for trouble by having a constant called CGI and a package called CGI... much confusion will ensue!
– Dancrumb
                May 11, 2011 at 13:43

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

constant variables are actually subroutines. Without package declaration, they must be in the main:: namespace. – yibe May 11, 2011 at 13:47 Bareword "URL" not allowed while "strict subs" in use at /var/www/cgi-bin/index.pl line 35. Execution of /var/www/cgi-bin/index.pl aborted due to compilation errors. – eouti May 11, 2011 at 13:48

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.

    There's really nothing arcane or magical about the constant pragma. It is purely syntactic sugar for creating constant subroutines. use constant X => 5; is exactly the same as sub X () {5}. In fact, I prefer the later because it serves as a reminder than the constants are in fact subroutines. – Eric Strom May 11, 2011 at 19:53 Maybe not 'magic' but certainly arcane, for the very reason that you prefer the latter: the sugar tries to make a subroutine look like a variable, but it doesn't behave like a variable in places that you would expect it to (like, within interpolated strings). – Dancrumb May 11, 2011 at 20:33

    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.