Accessing constants from another package/file.

E

ed

Hey all. I'm trying to use some constants that I have defined
using the "constant" module. I'm requiring the file into another package.
The package that uses the constant is used from "main".

The problem is that it isn't getting the value of the constant, it's
just using the string value of the constant name. Uh, don't know if that made
sense.

Example:
Below I want to refer to DE_Constants::DE_ERROR_WRONG_PARAMETER_TYPE
to get the error type, which should be the string "WRONG_PARAMETER_TYPE".
But it just gives me the string "DE_Constants::DE_ERROR_WRONG_PARAMETER_TYPE".

I've tried accessing it with and without the DE_Constanst:: prefix.
It doesn't work either way.

Right now I'm instantiating a LineFixer object from within the same file that
the LineFixer package is in. I do it at the bottom of the file after the closing
bracket for the "LineFixer" package, which I guess is assumed
to be "main".
Eventually LineFixer will reside in its own file and will get
'required' into the file where I want to use it.

Here's the relevant code:

#start myScript.pl
{ package LineFixer;

require 'ErrorManager.pl';
require 'DE_Constants.pl';

sub new
{
my $proto = shift;
my $class = ref($proto) || $proto;
my $this = {};
bless($this, $class);

$this->{pathToFileOrHandle} = shift;

$this->{isValidHandle} = 0;
$this->{FH} = undef;
$this->{fileContents} = undef;

$this->checkFileOrHandleArg();

return $this;
}


sub checkFileOrHandleArg
{
my $this = shift;

if ( $this->is_a_filehandle() )
{
$this->{isValidHandle} = 1;
return 1;
}
elsif ( $this->openFile() )
{ return 1;
}
else
{
$this->_createErrorObjOnce();
my $errorType = DE_Constants::DE_ERROR_WRONG_PARAMETER_TYPE;
my $errorMsg =
DE_Constants::DE_ERROR_WRONG_PARAMETER_TYPE.' in method LineFixer::new : '.
'You must pass either a filehandle or the valid path '.
'to a file to open for reading';
$this->{errorObj}->addToErrorList( {'type'=>$errorType, 'msg'=>$errorMsg} );

return 0;
}

}

etc........

} # end package "LineFixer"


$lineFixerObj = LineFixer->new('c:/temp/noname1');
$lineFixerObj->getFileContents();
$lineFixerObj->closeFile();

if ($lineFixerObj->hasErrors() )
{ print "it has errors!!\n";

$errorObj = $lineFixerObj->getErrorObj();
foreach my $errorArr (@{$errorObj->getErrors()})
{ print $errorArr->{'type'}."\n";
print $errorArr->{'msg'}."\n";
}
}

#end myScript.pl


#start DE_Constants.pl

{ package DE_Constants;
use constant DE_ERROR_WRONG_PARAMETER_TYPE => 'WRONG_PARAMETER_TYPE' ;
}
1;
#end DE_Constants.pl

------ Back to post --------
I can use the constant from inside "DE_Constants.pl".
By doing something like:
print DE_Constants::DE_ERROR_WRONG_PARAMETER_TYPE;

So I don't think the problem should be with the code that creates the constant.

Anyways, any help appreciated.

tia,
--ed
 
E

ed

Thanks for the advice purl gurl.

I think I've found the problem though. Although I still don't think I
fully understand it.
I forgot that perl automatically quotes barewords sometimes. I found a reference to this in the
docs for the constant module.
I got it to work by putting parenthesis at the end of the constant name.
Like this:
DE_Constants::DE_ERROR_WRONG_PARAMETER_TYPE()

But that seems strange to me(But then again a lot of things about perl seem strange to me :) ).
I don't know if there's a better solution, I'll have to see if I can find something
about it at perldoc.com .

thanks,
--ed
 
G

Giovanni Zezza

DE_Constants::DE_ERROR_WRONG_PARAMETER_TYPE()

But that seems strange to me(But then again a lot of things about perl seem strange to me :) ).

It may seem to you less strange, when you think that constants in the
Constant module are actually implemented as inlinable sub.

Being recognizable as sub, perl doesn't inline them at compile time (for it
hasn't encountered the sub declarations, yet), but is able to evaluate them
at run time, so everything works, in a way (the way being you aren't
actually using constants). At least, that is what I think is happening.
I don't know if there's a better solution

A better solution was provided by Anno Siegel: change 'require' to 'use'.

Ciao.
 
M

Martien Verbruggen

ed said:
Hey all. I'm trying to use some constants that I have defined
using the "constant" module. I'm requiring the file into another
package. The package that uses the constant is used from "main".

The problem is that it isn't getting the value of the constant,
it's just using the string value of the constant name. Uh, don't
know if that made sense.
[...]

Here's the relevant code:

#start myScript.pl
{ package LineFixer;

require 'ErrorManager.pl';
require 'DE_Constants.pl';

Change 'require' to 'use' here. Also, switch on strictures and warnings.
With those Perl would have pointed out the problem.

If you do that, you'll also have to change the pl files into modules
(and they have to have the extension pm). You also need to take into
account that import wil be called on the module. I agree that for
moderm code this is the best action to take.

If the files are just perl "libraries" in the old sense (a bunch of
subroutines packed together in a file), require is the correct way to
include them. If you ened them included at compile time, put it in a
BEGIN block.

OP: Have a read of the entry for C<use> in the perlfunc documentation,
as well as the entry for C<require>, which you presumably already
read.

I have no quibbles with the advice to siwtch on strictures and
warnings.

Martien
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top