Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Something that will let you use scalars but will prevent them being modified:
my $const = \"Don't touch me!"; my $num = \5.6; local *const2 = \"Another string"; local *num2 = \3.14159; print "[$$const][$$num][$const2][$num2]\n"; ### try and modify it eval{ $num2 = 3; }; print $@; print "$num2\n";

The first ones are not as pretty as dollar variables, and the second have a slight overhead of local, but I think that should give you some nice unmodifiable variables.

UPDATE:
Benchmarks are always good. Read em and weep:
#!/usr/bin/perl -w use Benchmark qw(timethese cmpthese countit timestr); use constant CONST_MOD => 3; sub CONST_SUB () { 3 }; my $const_my = 3; local $const_local = 3; my $const_ref = \3; local *const_alias = \3; print "mod[".CONST_MOD."]sub[".CONST_SUB."]\n" ."my[$const_my]local[$const_local]\n" ."ref[$$const_ref]alias[$const_alias]\n"; cmpthese (1_000_000, { CMod => sub { my $t = CONST_MOD }, CSub => sub { my $t = CONST_SUB }, CMy => sub { my $t = $const_my }, CLocal => sub { my $t = $const_local }, CRef => sub { my $t = $$const_ref }, CAlias => sub { my $t = $const_alias }, });
Produces the following:
mod[3]sub[3] my[3]local[3] ref[3]alias[3] Benchmark: timing 1000000 iterations of CAlias, CLocal, CMod, CMy, CRe +f, CSub... CAlias: 0 wallclock secs ( 0.79 usr + 0.00 sys = 0.79 CPU) @ 12 +65822.78/s (n=1000000) CLocal: 0 wallclock secs ( 0.80 usr + 0.00 sys = 0.80 CPU) @ 12 +50000.00/s (n=1000000) CMod: 2 wallclock secs ( 0.80 usr + 0.00 sys = 0.80 CPU) @ 12 +50000.00/s (n=1000000) CMy: 1 wallclock secs ( 0.75 usr + 0.00 sys = 0.75 CPU) @ 13 +33333.33/s (n=1000000) CRef: 0 wallclock secs ( 1.03 usr + 0.00 sys = 1.03 CPU) @ 97 +0873.79/s (n=1000000) CSub: 0 wallclock secs ( 0.81 usr + 0.01 sys = 0.82 CPU) @ 12 +19512.20/s (n=1000000) Rate CRef CSub CLocal CMod CAlias CMy CRef 970874/s -- -20% -22% -22% -23% -27% CSub 1219512/s 26% -- -2% -2% -4% -9% CLocal 1250000/s 29% 3% -- -0% -1% -6% CMod 1250000/s 29% 3% 0% -- -1% -6% CAlias 1265823/s 30% 4% 1% 1% -- -5% CMy 1333333/s 37% 9% 7% 7% 5% --
The only noticable differences are the my and the ref. The reference takes too long, the "my" is a little quicker but it is modifiable. All of the others are neck a neck, so, I'd vote for local *var=\23;

my @a=qw(random brilliant braindead); print $a[rand(@a)];

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

Title:
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!
  • 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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (14)
    As of 2015-07-28 11:32 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (254 votes), past polls