Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Does perl offer an auto incremental assigning of constants, like Swift?

by bartender1382 (Beadle)
on Apr 17, 2022 at 18:08 UTC ( #11143024=perlquestion: print w/replies, xml ) Need Help??

bartender1382 has asked for the wisdom of the Perl Monks concerning the following question:

Swift has a wonderful feature which auto enumerates values like this:

enum ButtType : Int { case inv = 0 case tut case sup case main case stop case reg case log case set case back case new case old case up case file }</p>

In perl I am doing this for errors

use constant { SSSUCCESS => 0, SSD001 => 1, SSD002 => 2, SSFAILEDRAWDB => 3, ... };</p>

Would be nice if there was something available, like in Swift. Otherwise every time I come up with a new error, to keep them related ones together I have to insert, renumber, etc.

Replies are listed 'Best First'.
Re: Does perl offer an auto incremental assigning of constants, like Swift?
by choroba (Archbishop) on Apr 17, 2022 at 18:15 UTC
    enum can do that any many more (e.g. bitmask constants).

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Thanks, should have thought first to check for a module
Re: Does perl offer an auto incremental assigning of constants, like Swift?
by tybalt89 (Prior) on Apr 17, 2022 at 19:06 UTC

    If you keep the numbers aligned in a column, vim is great at incrementing and decrementing columns of numbers.

Re: Does perl offer an auto incremental assigning of constants, like Swift?
by Perlbotics (Bishop) on Apr 18, 2022 at 19:07 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11143024]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2022-08-11 11:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?