Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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:

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


In reply to Re^3: replacing literals with constants by kcott
in thread replacing literals with constants by hexcoder

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-19 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found