Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

comment on

( #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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (3)
    As of 2019-05-23 03:02 GMT
    Find Nodes?
      Voting Booth?
      Do you enjoy 3D movies?

      Results (142 votes). Check out past polls.

      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!