The
readonly trait is intended primarily to mark a variable as readonly despite its underlying value being writable. It's the default for formal parameters, so while it's set on many variables, you won't actually see the word "readonly" terribly often unless you want to nail down a variable as a pseudo-constant over some scope.
What you will see more of is the constant declarator. Just
put "constant" where you would have put "my", like this:
constant $foo = 12;
This declares a true compile-time constant, lexically scoped. Any place the compiler sees
$foo, it's free to substitute in 12. The item declared need not have a sigil on the front as long as it's a valid identifier:
constant Int foo = 12;
That just makes it a bit harder to interpolate. Instead of
$foo you'd have to say
{foo}. But any valid identifier also includes Unicode, so you can even say:
constant Num π = atan(2,2) * 4;
Note that the expression is evaluated at compile time, whereas
readonly on a normal
my evaluates at run time.
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.