Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I'm trying to create a read-only "multi-dimensional" constant using the built-in constant pragma. I know that there's the Readonly module (amongst others), but I'm trying to stick to basics.

Using anonymous list references doesn't really work, because they can be modified, as documented:

Even though a reference may be declared as a constant, the reference may point to data which may be changed, as this code shows.
use constant ARRAY => [ 1,2,3,4 ]; print ARRAY->[1]; ARRAY->[1] = " be changed"; print ARRAY->[1];

Okay, so I'll use some sort of indirection, I thought, and came up with something like this:

use strict; use warnings; use Data::Dumper; use constant INVALID_DATA => ( q{invalid}, 0 ); use constant ADD_DATA => ( q{add}, 1 ); use constant REMOVE_DATA => ( q{remove}, 2 ); use constant MODES => ( \&ADD_DATA, \&REMOVE_DATA ); print {*STDERR} "Dumping MODES: " . Dumper ((MODES)); 1;
… but, this doesn't quite do what I expected. Output:
Dumping MODES: $VAR1 = sub { "DUMMY" }; $VAR2 = sub { "DUMMY" };
I was under the impression that constants are really subs that I could take the reference of, but this doesn't seem to be the case.

Alternatively, I've tried taking direct references, but this is... just merging the lists (which is obviously bad to begin with and what I wanted to avoid by using references) in a weird "reference all elements individually" way:

use constant MODES => ( \ADD_DATA, \REMOVE_DATA );
leading to
Dumping MODES: $VAR1 = \'add'; $VAR2 = \1; $VAR3 = \'remove'; $VAR4 = \2;

Is there any proper way to do this?


In reply to Multi-dimensional constants by Ionic

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-04-24 06:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found