Come for the quick hacks, stay for the epiphanies. | |
PerlMonks |
Re^3: replacing literals with constantsby kcott (Archbishop) |
on May 14, 2022 at 13:17 UTC ( [id://11143897]=note: print w/replies, xml ) | Need Help?? |
G'day hexcoder, It is a convention, but not a requirement, to use all uppercase for constants. This makes it easier to identify constants in your code: TEN stands out more than ten. See the examples in constant. "So, I will always use prototypes for constants from now on." For simple values (e.g. numbers, strings, etc.) I tend to use constant. For example:
This is a lot simpler than coding:
I do use subroutines with an empty prototype on occasions. This would be when more complicated code is required. Consider the following example that creates a singleton object.
Now, OBJ can be used (read-only) wherever the singleton instance is required, but cannot be modified; and %constructor_args cannot be accessed anywhere else in the code. That's a somewhat contrived example; however, I hope it gives you an idea of where a subroutine with an empty prototype might be used for a constant (where use of the constant pragma might be unwieldy or impractical). Here (in the spoiler) is an actual implementation of that. It's still rather contrived, as there are much better ways of doing this, but it does show the process. In this example, a single Text::CSV object is used as the value of a constant which processes two CSV (pipe-separated) files. (You might want to see Inline::Files if you're unfamiliar with that module.) <Reveal this spoiler or all in this thread>
— Ken
In Section
Seekers of Perl Wisdom
|
|