Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: replacing literals with constants

by hexcoder (Curate)
on May 14, 2022 at 09:20 UTC ( [id://11143895]=note: print w/replies, xml ) Need Help??


in reply to Re: replacing literals with constants
in thread replacing literals with constants

Thanks a lot!
So, I will always use prototypes for constants from now on.

Replies are listed 'Best First'.
Re^3: replacing literals with constants
by kcott (Archbishop) on May 14, 2022 at 13:17 UTC

    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:

    use constant { ONE => 1, TWO => 2, ... NINE => 9, TEN => 10, };

    This is a lot simpler than coding:

    sub ONE () { 1; } sub TWO () { 2; } ... sub NINE () { 9; } sub TEN () { 10; }

    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.

    { my %constructor_args; BEGIN { # ... code to populate %constructor_args ... } sub OBJ () { Some::Module::->new(\%constructor_args); } }

    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.)

    — Ken

Re^3: replacing literals with constants
by LanX (Saint) on May 14, 2022 at 22:35 UTC
    why not just use constant which is self describing?

    many maintainers - like you - will struggle to understand the "magic" of empty prototypes.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11143895]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-29 08:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found