The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

OP::Enum - C style enumerated types in Perl

DESCRIPTION

This module emulates the interface of enum.pm, sans support for bitmasks. Unlike enum.pm, the symbols generated by this module are actual Perl constants, and may be imported and exported as such by external modules.

ENUMERATIONS

  • OP::Enum::Bool

    Provide false and true enumerated constants

  • OP::Enum::Consol

    Constant enumeration of supported series consolidation methods

  • OP::Enum::DBIType

    Constant enumeration of supported database types

  • OP::Enum::Inter

    Constant enumeration of supported series interpolation methods

  • OP::Enum::State

    Criticality enumeration. Exports "Nagios-style" states: OK (0), Warn (1), and Crit (2)

  • OP::Enum::StatType

    Constant enumeration of supported series statistic handling methods (gauge, counter, derivative)

SYNOPSIS

The following is borrowed from the perldoc for enum.pm:

  use OP::Enum qw(Sun Mon Tue Wed Thu Fri Sat);
  # Sun == 0, Mon == 1, etc

  use OP::Enum qw(Forty=40 FortyOne Five=5 Six Seven);
  # Yes, you can change the start indexs at any time as in C

  use OP::Enum qw(:Prefix_ One Two Three);
  ## Creates Prefix_One, Prefix_Two, Prefix_Three

  use OP::Enum qw(:Letters_ A..Z);
  ## Creates Letters_A, Letters_B, Letters_C, ...

  use OP::Enum qw(
      :Months_=0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
      :Days_=0   Sun Mon Tue Wed Thu Fri Sat
      :Letters_=20 A..Z
  );
  ## Prefixes can be changed mid list and can have index changes too

Bitmask support is not currently implemented.

Basic Usage

Create a quick package of enumerated constants:

  #
  # File: Month.pm
  #
  package Month;

  use OP::Enum qw|
    jan feb mar apr may jun jul aug sep oct nov dec
  |;

  1;

Meanwhile, in caller:

  #
  # File: hangover.pl
  #
  use Month;

  ...

  my $month = getMonth();

  if ( $month == Month::jan ) {
    print "Happy New Year\n";
  }

Auto Export

Same usage as the Month.pm example above, with an extra block of code eval'd for Exporter.

  #
  # File: DayOfWeek.pm
  #
  package DayOfWeek;

  use OP::Enum qw| sun mon tue wed thu fri sat |;

  eval { @EXPORT = @EXPORT_OK };

  1;

Meanwhile, in caller:

  #
  # File: pizza-reminder.pl
  #
  use DayOfWeek;

  ...

  my $day = getDayOfWeek();

  if ( $day == fri ) {
    print "It's pizza day!\n";
  }

SEE ALSO

Exporter, constant, enum

This file is part of OP.