N
newsbot
First my disclaimer... 5 Years of Perl and I've never really needed to
do what I am asking here (mainly because of how Perl works so well
without it, I guess), but I need to do it now, and I'm a little
surprised that it doesn't work the way I thought it would.
I want to be able to hold, in one place, a file/module of constants
that I can use in any other module in a project. Kind of like header
files (.h) in C:
#define FOO "bar"
Then I include that .h file in all .c source files. So in Perl, I did
this:
=== TestK.pm ===
use constant TEST1 => 'Test1';
use constant TEST2 => 'FooBar';
1;
=== TestMod.pm ===
#!/usr/bin/perl
$|++;
use strict;
use warnings qw( all );
use TestK;
sub Test {
print __PACKAGE__ . ": TEST1 = " . TEST1 . "\n";
print __PACKAGE__ . ": TEST2 = " . TEST2 . "\n";
}
1;
=== test.pl ===
#!/usr/bin/perl
$|++;
use strict;
use warnings qw( all );
use TestK;
use TestMod;
TestMod::Test();
print __PACKAGE__ . ": TEST1 = " . TEST1 . "\n";
print __PACKAGE__ . ": TEST2 = " . TEST2 . "\n";
__END__
I *don't* get what I expect here. Instead, I get:
Bareword "TEST1" not allowed while "strict subs" in use at test.pl line
12.
Bareword "TEST2" not allowed while "strict subs" in use at test.pl line
13.
It's like I can't "use TestK" in both test.pl and TestMod.pm. If I
comment out the "use TestMod;" and the TestMod::Test() call in test.pl,
the print statments in test.pl are fine. If I comment out the print
statements in test.pl and the "use TestK;" in test.pl, then the call to
TestMod::Test() in test.pl is fine. But I can't do both.
Documentation on "use constants" says that constants are package
scoped. But without providing a package name for the constants in
TestK.pm, I would expect the constants to be scoped to each package
they were used in, so I would get:
main::TEST1
main::TEST2
TestMod::TEST1
TestMod::TEST2
Obviously, this is NOT the case. And again, I don't recall ever
needing to do this, so I'm a little amazed. I don't recall (nor find
on Google in c.l.p.m) any real discussion of using constants in
multiple packages. (I think mainly because use of "constants" in Perl
as they are in C isn't in high use.)
Can anyone shed light on the above?
Thanks!
-ceo
do what I am asking here (mainly because of how Perl works so well
without it, I guess), but I need to do it now, and I'm a little
surprised that it doesn't work the way I thought it would.
I want to be able to hold, in one place, a file/module of constants
that I can use in any other module in a project. Kind of like header
files (.h) in C:
#define FOO "bar"
Then I include that .h file in all .c source files. So in Perl, I did
this:
=== TestK.pm ===
use constant TEST1 => 'Test1';
use constant TEST2 => 'FooBar';
1;
=== TestMod.pm ===
#!/usr/bin/perl
$|++;
use strict;
use warnings qw( all );
use TestK;
sub Test {
print __PACKAGE__ . ": TEST1 = " . TEST1 . "\n";
print __PACKAGE__ . ": TEST2 = " . TEST2 . "\n";
}
1;
=== test.pl ===
#!/usr/bin/perl
$|++;
use strict;
use warnings qw( all );
use TestK;
use TestMod;
TestMod::Test();
print __PACKAGE__ . ": TEST1 = " . TEST1 . "\n";
print __PACKAGE__ . ": TEST2 = " . TEST2 . "\n";
__END__
I *don't* get what I expect here. Instead, I get:
Bareword "TEST1" not allowed while "strict subs" in use at test.pl line
12.
Bareword "TEST2" not allowed while "strict subs" in use at test.pl line
13.
It's like I can't "use TestK" in both test.pl and TestMod.pm. If I
comment out the "use TestMod;" and the TestMod::Test() call in test.pl,
the print statments in test.pl are fine. If I comment out the print
statements in test.pl and the "use TestK;" in test.pl, then the call to
TestMod::Test() in test.pl is fine. But I can't do both.
Documentation on "use constants" says that constants are package
scoped. But without providing a package name for the constants in
TestK.pm, I would expect the constants to be scoped to each package
they were used in, so I would get:
main::TEST1
main::TEST2
TestMod::TEST1
TestMod::TEST2
Obviously, this is NOT the case. And again, I don't recall ever
needing to do this, so I'm a little amazed. I don't recall (nor find
on Google in c.l.p.m) any real discussion of using constants in
multiple packages. (I think mainly because use of "constants" in Perl
as they are in C isn't in high use.)
Can anyone shed light on the above?
Thanks!
-ceo