Accessing constants from another package/file.

Discussion in 'Perl Misc' started by ed, Jun 25, 2003.

  1. ed

    ed Guest

    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
    ed, Jun 25, 2003
    #1
    1. Advertising

  2. ed

    ed Guest

    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
    ed, Jun 25, 2003
    #2
    1. Advertising

  3. ed, nel messaggio <>, scriveva:

    > 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.
    Giovanni Zezza, Jun 25, 2003
    #3
  4. On 25 Jun 2003 08:37:35 GMT,
    Anno Siegel <-berlin.de> wrote:
    > ed <> wrote in comp.lang.perl.misc:
    >> 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
    --
    |
    Martien Verbruggen | True seekers can always find something to
    Trading Post Australia | believe in.
    |
    Martien Verbruggen, Jun 26, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Agoston Bejo

    Retrieve package constants

    Agoston Bejo, Sep 1, 2004, in forum: ASP General
    Replies:
    0
    Views:
    110
    Agoston Bejo
    Sep 1, 2004
  2. Replies:
    1
    Views:
    101
  3. Terry
    Replies:
    13
    Views:
    301
    Brian McCauley
    Dec 19, 2004
  4. Sven-Thorsten Fahrbach
    Replies:
    3
    Views:
    100
    Sven-Thorsten Fahrbach
    Jul 30, 2005
  5. Replies:
    15
    Views:
    204
    comp.llang.perl.moderated
    May 14, 2008
Loading...

Share This Page