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 390389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:57:35+00:00 2026-05-12T15:57:35+00:00

I have done some digging through perldoc and the O’Reilly books but haven’t found

  • 0

I have done some digging through perldoc and the O’Reilly books but haven’t found any way to do this. Am I relegated to using something like Readonly?

UPDATE:

I have nothing against Readonly. I just wanted to be able to do something like PHP’s constant().

Example if Perl had constant():

use constant {
  FIELD_EXAMPLE_O => 345,
  FIELD_EXAMPLE_L => 25
};

my $var = 'EXAMPLE';
my $c = 'FIELD_' . $var . '_L';
my $value = constant($c);

# $value is 25

If Readonly is the best way to do it, then I shall use it.

  • 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-12T15:57:35+00:00Added an answer on May 12, 2026 at 3:57 pm

    What’s wrong with Readonly?

    If it’s too slow, you can supplement it with Readonly:XS. But if you don’t like Readonly, there’s always the older constant.

    use constant PI => 3.14159265;
    

    Just remember

    1. They work like subs, so they don’t interpolate without work.
    2. If you want to create multiple constants in one statement, you need to pass a hash reference.

      use constant { PI => 3.14159265
                   , E  => 2.71828183
                   };
      

    From Your Example:

    Judging from your example, there’s no reason why a readonly hash couldn’t do the same thing.

    Readonly::Hash my %field_example => { L => 25, O => 345 };
    

    Then you could use it anywhere you’d want to cobble the constant:

    print "The example is $field_example{$var}\n";
    

    OR you could do it this way:

    Readonly::Hash my %field 
        => { example => { L => 25,     O => 345 }
           , name    => { L => 'Lion', O => 'ocelot' }
           };
    

    And call it this way:

    $field{$var}{L};
    

    You can get a lot of mileage about not trying to make a language do what it has better support for doing in another way.

    Cognate to PHP constant

    However, if you want to do it that way, then my suggestion is that the following sub is a way of doing the same ( and avoids an eval ):

    sub read_constant { 
        use Symbol qw<qualify_to_ref>;
        my $name = join( '', @_ ); # no need to concatenate before passing
        my $constant;
        # use the first that works: calling package and then "" (main)
        for my $pkg ( scalar( caller ), "" ) { 
            # get symbol reference
            my $symb_ref = qualify_to_ref( $name, $pkg );
            # get the code slot
            $constant    = *{$symb_ref}{CODE};
            last if $constant;
        }
        return unless $constant;
        # call the sub named
        return $constant->();
    }
    

    You’d call it like this:

    $value = read_constant( 'FIELD_', $var, 'L' );
    

    One last thing, is that you could even put a test in front to make sure that it is only a all cap string:

    Carp::croak "Invalid constant name '$name'" if $name =~ /[^\p{UpperCase}_]/;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have done some research into this topic, but found no relevant answers. What I
I have done some looking and I found this: http://www.codeproject.com/Articles/14439/The-ScrollableListBox-Custom-Control-for-ASP-NET-2 but to me it
i have done some code in jquery but where i commented in this bellow
I have done some digging and found some code that can run the application
Have done some working samples using Backbone Router, but is there a way to
I have done some research into this, but I am not particularly experienced with
I have done some digging out here and am still a little unsure how
Have done some research and found some stuff that may be helpful. I would
I have done some stuff in jQuery and Javascript before, but unfortunately I am
I have done some research, and majority of the examples I have found use

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.