Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: replacing literals with constants

by Corion (Patriarch)
on May 14, 2022 at 09:15 UTC ( [id://11143894]=note: print w/replies, xml ) Need Help??


in reply to replacing literals with constants

Deparsing the code shows what Perl sees (in tmp.pl):

#!/usr/bin/perl -w use strict; sub ten { 10 } print "9 .. 10 - 1 with constant\n"; for my $i (9 .. ten - 1) { print $i, "\n"; } print "-> unexpected\n\n";

gives when deparsed:

corion@outerlimits:~/Projekte$ perl -MO=Deparse tmp.pl BEGIN { $^W = 1; } use strict; sub ten { 10; } print "9 .. 10 - 1 with constant\n"; foreach my $i (9 .. ten(-1)) { print $i, "\n"; } print "-> unexpected\n\n"; tmp.pl syntax OK

Since you don't have a prototype on your constant, the -1 gets interpreted as an argument to your subroutine call ten. The fix is to use a prototype, or to use the constant module, which does the same:

sub ten() { 10 };

... or ...

use constant ten => 10;

Replies are listed 'Best First'.
Re^2: replacing literals with constants
by hexcoder (Curate) on May 14, 2022 at 09:20 UTC
    Thanks a lot!
    So, I will always use prototypes for constants from now on.

      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

      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://11143894]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-03-29 16:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found