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:E_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:E_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:E_ERROR_WRONG_PARAMETER_TYPE;
my $errorMsg =
DE_Constants:E_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:E_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
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:E_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:E_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:E_ERROR_WRONG_PARAMETER_TYPE;
my $errorMsg =
DE_Constants:E_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:E_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