Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1054069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:20:54+00:00 2026-05-16T17:20:54+00:00

I am experiencing a problem with using a constant defined in a configuration file.

  • 0

I am experiencing a problem with using a constant defined in a configuration file.
This is my package:

package myPackage;
require "APIconfig.pl";
APIconfig::import(APIconfig);

use constant SERVICE_URL => APIconfig::SERVICE_URL();

The configuration looks like this:

package APIconfig;
use constant SERVICE_URL => 'http://api.example.org/blah';
1;

When running this code, I get the following error:

Undefined subroutine &APIconfig::SERVICE_URL called at API.pl line 4.

I cannot use ‘use’ instead of ‘require’ because this expects the configuration file to be named .pm, and it’s called .pl on a lot of servers on our network.
How can I use the package without renaming the file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:20:54+00:00Added an answer on May 16, 2026 at 5:20 pm

    There are two differences between ‘use’ and ‘require’. One of them affects your current problem, the other doesn’t. Unfortunately you are working around one that has no effect.

    The differences are:

    1/ ‘use’ calls the import() function, ‘require’ doesn’t.

    2/ ‘use’ happens at compile time, ‘require’ happens at runtime.

    You’re working around the fact that ‘require’ doesn’t call import() by calling it explicitly. This has no effect as your module doesn’t export any symbols and doesn’t have an import() subroutine.

    You’re not working around the fact that ‘use’ statements are executed at runtime. The problem is that “use constant SERVICE_URL => APIconfig::SERVICE_URL();” is executed at compile time and your ‘require’ hasn’t run by then so myPackage knows nothing about APIconfig.

    The (nasty, hacky) solution is to put the ‘require’ statement into a BEGIN block – to force it to be executed at compile time. You’ll also want to remove the call to import() as that gives a runtime error (due to the absence of the subroutine).

    The test files that I used to work this out are as follows:

    $ cat APIconfig.pl 
    package APIconfig;
    use constant SERVICE_URL => 'http://api.example.org/blah';
    1;
    
    $ cat api.pl 
    #!/usr/bin/perl
    
    package myPackage;
    BEGIN {
      require "APIconfig.pl";
    }
    # APIconfig::import(APIconfig);
    
    use constant SERVICE_URL => APIconfig::SERVICE_URL();
    
    print SERVICE_URL, "\n";
    $ ./api.pl 
    http://api.example.org/blah
    

    The real solution is to rewrite APIconfig as a real module. You hint that you know that, but that environmental issues prevent you taking this approach. I highly recommend trying to work around those issues and doing things correctly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am experiencing this problem with Node.js express framework 3.0: https://github.com/visionmedia/express/issues/1187 I've been using
I am using cakephp 1.3.X . I am experiencing some problem with url routing
I'm using Django 1.4 and experiencing an unusual problem with hidden rows in my
We are experiencing a performance problem when communicating with the active directory using System.DirectoryServices.
I am experiencing some problem on UTF-8 Encoding. I have a CSV file and
I am experiencing a rather strange problem using PHP 5.3's date diff function to
I'm using Silverlight 4 and I'm experiencing the following problem: First off, the code:
using yslow on a project I am experiencing a problem I can not solve.
I'm experiencing a problem with MySQL's GROUP_CONCAT function. I will illustrate my problem using
i'm currently experiencing a little problem. I'm using nhibernate with around 40 entities mapped

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.