T
Terry
Hello all, I'm having trouble with packages and constants. What I'd
like to have is a single .pm for all constants used by the application.
The application will have other .pm files as well, and I'd like for them
to have access to the constants as well. I thought the following would
do what I want, but fails miserably:
#### file: ./main
#!/usr/bin/perl
use warnings;
use strict;
use One::Two::Constants;
use One::Two::Obj;
print "Constant PI is: " , PI , "\n";
my $obj = Obj->new();
print "Object member PI is: " , $obj->{'pi'} , "\n";
#### file ./One/Two/Constants.pm
package One::Two::Constants;
use constant { PI => 3.1415 };
1;
#### file ./One/Two/Obj.pm
package One::Two::Obj;
use warnings;
use strict;
use One::Two::Constants;
sub new {
my $class = shift;
my $self = {
some => 'thing' ,
more => 'stuff' ,
pi => PI
};
return bless $self , $class;
}
1;
$ ./main
Bareword "PI" not allowed while "strict subs" in use at One/Two/Obj.pm
line 10.
Compilation failed in require at ./main line 7.
BEGIN failed--compilation aborted at ./main line 7.
The only way I've been able to get this to run is by removing the
package declaration from Constants.pm, changing the package declaration
of Obj.pm to just 'package Obj', and by referring to 'PI' inside of
Obj.pm as 'main:
I'. Clearly this is a poor approach.
All I want is all constants kept in one place, able to be referred to
from anywhere else. Any suggestions?
As a side note, aren't package names supposed to be fully qualified?
i.e, aren't .pm file supposed to begin with something like:
package One::Two::Something;
Importing with 'use' only seems to work when packages are declared with
only their basename:
package Something;
Thank you for your help.
-Terry
like to have is a single .pm for all constants used by the application.
The application will have other .pm files as well, and I'd like for them
to have access to the constants as well. I thought the following would
do what I want, but fails miserably:
#### file: ./main
#!/usr/bin/perl
use warnings;
use strict;
use One::Two::Constants;
use One::Two::Obj;
print "Constant PI is: " , PI , "\n";
my $obj = Obj->new();
print "Object member PI is: " , $obj->{'pi'} , "\n";
#### file ./One/Two/Constants.pm
package One::Two::Constants;
use constant { PI => 3.1415 };
1;
#### file ./One/Two/Obj.pm
package One::Two::Obj;
use warnings;
use strict;
use One::Two::Constants;
sub new {
my $class = shift;
my $self = {
some => 'thing' ,
more => 'stuff' ,
pi => PI
};
return bless $self , $class;
}
1;
$ ./main
Bareword "PI" not allowed while "strict subs" in use at One/Two/Obj.pm
line 10.
Compilation failed in require at ./main line 7.
BEGIN failed--compilation aborted at ./main line 7.
The only way I've been able to get this to run is by removing the
package declaration from Constants.pm, changing the package declaration
of Obj.pm to just 'package Obj', and by referring to 'PI' inside of
Obj.pm as 'main:
All I want is all constants kept in one place, able to be referred to
from anywhere else. Any suggestions?
As a side note, aren't package names supposed to be fully qualified?
i.e, aren't .pm file supposed to begin with something like:
package One::Two::Something;
Importing with 'use' only seems to work when packages are declared with
only their basename:
package Something;
Thank you for your help.
-Terry