Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
'use constant' doesn't appear to be terribly popualar, as 'my' does effectively the same thing, without the child-proof safety locks that prevent you from modifying variables declared that way.

Instead of declaring a whole whack of constants using my(), such as:
my ($filename) = "/path/to/file"; my ($filemode) = "immolate"; my ($opmode) = "seek"; my ($filetype) = "image/gif";
You could always convert this to a hash-structure, such as:
my (%config) = ( filename => "/path/to/file", filemode => "immolate", opmode => "seek", filetype => "image/gif", );
This has the advantage of allowing you to declare virtually unlimited constants, without having to explicitly declare them on their own my() line. Additionally, you can load these from a file quickly and easily (i.e. an INI-type file, an Apache-style file, or something else) such that you can vary the configuration without modifying the program itself.

Of course, using a hash-lookup is slower than a regular variable, but the price you pay would depend on frequency of use, and in practice, should be so slight as to really be irrelevant. In most cases where a constant is used frequently, such as inside a loop, you would expect to see something like:
my ($thing) = $config{'thing_to_mash'}; for (my $x = 0; $x < 100_000_000; $x++) { DoCrazyStuff($thing, $x, Foo($x)); }
In this admittedly simple example, the hash is only used once, and the variable itself is repeatedly used.

If you really don't trust yourself to keep your const's constant, and to not modify them in any way, write a function to extract parameters from it, such as:     sub Config { return $config{$_[0]}; } Then you could keep %config in some sort of codeblock or module which nobody has access too. This is kind of extreme, though.

In reply to Re: my $var; vs. use constant VAR = ''; by tadman
in thread my $var = ''; vs. use constant VAR =&gt; ''; by antihero

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 having a coffee break in the Monastery: (3)
As of 2024-04-18 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found