Just a try...

use strict; use warnings; use Carp; use Scalar::Util qw(looks_like_number); sub make_const { my ( $package, $start_i, $inc, @names ) = @_; croak "Expecting at least 3 parameters!" if @_ < 3; croak "'$package' - invalid package name!" if $package !~ /^(\w+) +(::\w+)*$/; croak "start_i = '$start_i' is not a number!" unless looks_like_numb +er( $start_i ); croak "inc = '$inc' is not a number!" unless looks_like_numb +er( $inc ); carp "inc=0 probably makes no sense" unless $inc; foreach ( @names ) { eval "sub $package\::$_ { return $start_i }"; $start_i += $inc; } } BEGIN { make_const( 'main', 0, 1, qw(inv tut sup main stop reg log set bac +k new old up file) ); #-- better avoid collisions by not using 'main' / uppercase const +ants are common make_const( 'Enum', 10, -2, qw(A B C D E) ); # croaks: make_const( 'X {1}; system("cat /etc/passwd"); sub Enum', + 10, -2, qw(A B C D E) ); } print "1st list of enums: ", join(', ', inv, tut, up, file ), + "\n"; print "2nd list of enums: ", join(', ', Enum::A, Enum::B, Enum::E ), + "\n";

Result:

1st list of enums: 0, 1, 11, 12 2nd list of enums: 10, 8, 2


In reply to Re: Does perl offer an auto incremental assigning of constants, like Swift? by Perlbotics
in thread Does perl offer an auto incremental assigning of constants, like Swift? by bartender1382

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



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.