| 1 | package enum::prefix; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | use 5.006; | ||
| 5 | our $VERSION = '0.03'; | ||
| 6 | |||
| 7 | my $ident = '[^\W_0-9]\w*'; | ||
| 8 | |||
| 9 | sub import | ||
| 10 | { | ||
| 11 | my $class = shift; | ||
| 12 | my $prefix = shift; | ||
| 13 | my $name_sub_suffix = shift; | ||
| 14 | @_ or return; | ||
| 15 | |||
| 16 | my $pkg = caller() . '::'; | ||
| 17 | |||
| 18 | my $index = 0; | ||
| 19 | |||
| 20 | my @enum; | ||
| 21 | no strict 'refs'; | ||
| 22 | for my $name (@_) | ||
| 23 | { | ||
| 24 | if ( $name =~ /^$ident$/o ) | ||
| 25 | { | ||
| 26 | my $n = $index; | ||
| 27 | $index++; | ||
| 28 | *{ $pkg . $prefix . $name } = sub(){ $n }; | ||
| 29 | $enum[$n] = $name; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | *{$pkg. $prefix . $name_sub_suffix } = sub($) { $enum[ $_[0] ] }; | ||
| 33 | } | ||
| 34 | |||
| 35 | 1; | ||
| 36 | |||
| 37 | __END__ |