Object-PadX-Enum

 view release on metacpan or  search on metacpan

lib/Object/PadX/Enum.pm  view on Meta::CPAN

=over 4

=item *

User C<field>s require explicit C<:param> if you intend to set them via
C<item> args. C<Object::PadX::Enum> does I<not> inject C<:param> automatically.

=item *

Singletons are constructed at the runtime of the compilation unit that
contains the C<enum> declaration, after that unit's C<UNITCHECK> phase. They
are therefore not visible from earlier C<BEGIN>/C<UNITCHECK> blocks of the
same unit. Normal runtime code (including code inside C<do BLOCK> and
C<eval "STRING"> blocks executed during main runtime) sees them as expected.

=item *

C<enum>-level C<:abstract>, C<:strict>, C<:repr> and C<:lexical_new> are not
supported. See the description of the C<enum> keyword above for the rationale;
C<:isa> and C<:does> I<are> supported.

=item *

lib/Object/PadX/Enum.xs  view on Meta::CPAN

   for (int i = 0; i < nattrs; i++) {
      AV *pair = newAV();
      av_push(pair, SvREFCNT_inc(args[2 + i]->attr.name));
      SV *value = args[2 + i]->attr.value;
      av_push(pair, value ? SvREFCNT_inc(value) : newSV(0));
      av_push(attrs_av, newRV_noinc((SV *)pair));
   }
   SV *attrs_ref = sv_2mortal(newRV_noinc((SV *)attrs_av));

   /* Drive Object::Pad::MOP::Class->begin_class via the Perl helper. This
    * sets compclassmeta, registers UNITCHECK auto-seal, and adds $ordinal.
    */
   {
      dSP;
      ENTER;
      SAVETMPS;
      PUSHMARK(SP);
      XPUSHs(packagename);
      XPUSHs(attrs_ref);
      PUTBACK;
      call_pv("Object::PadX::Enum::_begin_enum", G_VOID | G_DISCARD);

t/05-eval-and-do.t  view on Meta::CPAN

#!perl
use v5.22;
use warnings;

use Test2::V0;

# String-eval contains its own compilation unit; UNITCHECK fires at end of
# eval-compile, runtime follows inside the eval. Singletons should be usable
# from the same eval immediately after the enum block.
{
   my $ord = eval q{
      use Object::PadX::Enum;
      enum InEval {
         item A;
         item B;
      }
      InEval->B->ordinal;
   };
   ok( !$@, 'no eval error' ) or diag $@;
   is( $ord, 1, 'eval-string enum: InEval->B->ordinal' );
}

# do BLOCK runs at runtime within the enclosing unit, after that unit's
# UNITCHECK. The item/finalize ops execute when the block runs.
use Object::PadX::Enum;

my $result = do {
   enum InDo {
      item X;
      item Y;
      item Z;
   }
   [ map { $_->ordinal } InDo->values ];
};



( run in 0.602 second using v1.01-cache-2.11-cpan-9581c071862 )