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

  • SEARCH
  • Home
  • 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 627893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:34:39+00:00 2026-05-13T19:34:39+00:00

I have a set of constants declared in Perl: use constant C1 => 111;

  • 0

I have a set of constants declared in Perl:

   use constant C1 => 111;
   use constant C2 => 222;
   ..
   use constant C9 => 999;
   my $which_constant = "C2";

How do I construct a Perl expression which, based on $which_constant, derives the value of a constant named with the value of this variable – e.g. “222”.

Please note that I can not change any of the conditions above – they are a simplification of a real scenario: I have a module (which I have no control over) from which these constants are imported. The `name of one of the constants is supplied by the user from command line. I need to access the appropriate constants’ value.

I’ve been beating my head against the wall (mostly around all sorts of weird glob constructs) but none of them work.

P.S. If the solution accesses the constants inside their native module – say My::Constants::C2 (without needing to import them), even better, but not necessary – I can import the correct constants into main:: easily using My::Constants->import($which_constant). and yes, to top it off, te constants are NOT exported by default thus needing the explicit import() call.

Some of the things I tried:

  • main::$which_constant – syntax error

  • main::${which_constant} – syntax error

  • ${*$which_constant} – returns empty value

  • *$which_constant – returns “*main::C2”

  • ${*${*which_constant}} – empty

  • 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-13T19:34:39+00:00Added an answer on May 13, 2026 at 7:34 pm

    Constants defined by constant.pm are just subroutines. You can use method invocation syntax if you have the name of the constant in a string:

    #!/usr/bin/perl -l
    
    use strict; use warnings;
    use constant C1 => 111;
    use constant C2 => 222;
    
    print __PACKAGE__->$_ for qw( C1 C2 );
    # or print main->$_ for qw( C1 C2 );
    

    This way, if you try to use a constant which is not defined, you will get an error.

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

Sidebar

Related Questions

I have a Perl module that I have declared some constants: use constant BASE_PATH
I have an SQL database set up in which I would like to use
in my config.php where i have all constants i set the PATH to a
I have set up 2 servers which are linked. From my LOCAL SERVER, I
I have a set of classes which extend an abstract class. The abstract class
I have a script that I use to construct both the tables and stored
This is the code from stored procedure of SQL server. I have declared constant
i have set of constants. Enum will be the best option to define set
I have set constants for database in connect.php. connect.php define('DB_HOST','localhost'); define('DB_NAME','dbname'); define('DB_USER','dbuser'); define('DB_PASSWORD','dbpass'); Here
I have a table that containts a set of columns one of it is

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.