Stanisław T. Findeisen wrote:
My application consists of several files. Some of them are to be run as
CGI scripts, the others from command line. There is also a "common" file
with constants and shared routines.

First: Please disregard my theory about concatenation. Concatenation is ok, which is clearly stated in "perldoc constant".

The question is: what is the most standard way to export constants from
that common file so that they could be used in the other files?

Since the files share the same package, no exporting is needed.

Here is a minimal example:

- -------- one.pl --------
package MyPackage;

use warnings;
use strict;

use constant {
~    MY_FIRST_CONSTANT => 'hello world!'
};

*MY_SECOND_CONSTANT = [1, 3, 7];
*MY_SECOND_CONSTANT = \729;

sub routine1 {
~    print "Hello from routine1! $MyPackage::MY_SECOND_CONSTANT\n";
}

# This is to satisfy require()
1;

- -------- two.pl --------
package MyPackage;

use warnings;
use strict;

require 'one.pl';

print 'The 1st constant is: ' . MyPackage->MY_FIRST_CONSTANT . "\n";
print 'The 2nd constant is: ' . *MY_SECOND_CONSTANT . "\n";
print '             SCALAR: ' . ${*MY_SECOND_CONSTANT{SCALAR}} . "\n";
print '              ARRAY: ' . join(', ',
@{*MY_SECOND_CONSTANT{ARRAY}}) . "\n";

MyPackage->routine1;

I (now) believe that the problem is about compile time vs. runtime. When two.pl is compiled, one.pl has not yet been required.

Two possible solutions:

1) Put the require statement in a BEGIN block.

    BEGIN { require 'one.pl' }

2) Convert one.pl to a module (one.pm) and use() it.

    use one;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to