The following implements an enum a la C. It probably has been done before and better. I welcome pointer, but this is not my point. Here I am obliged to use twice the same pattern match. I can't even use qr// because I would have no way (at least in perl5) to get at the capture. TheDamian may had wanted to save away the captures to restore them but $1, $2... are read-only as far as the end-programmer is concerned. I have not peeked at Damian's code but this Switch seems to me yet another case for the scopeless eval.

I already discussed at unscoped eval, but there will never be enough shameless plugs in perlmonks for Perl6 and parrot.

#! /usr/bin/perl use strict; use Switch 'Perl6'; use Carp; sub enum { my @enum = split /,/, $_[0]; my $i = 0; for (@enum ) { given ($_) { when m/([A-Z]+\d*)(?:\s*=\s*(\d)+)?/i { m/([A-Z]+\d*)(?:\s*=\s*(\d)+)?/i; no strict; $i = $2 if defined $2; eval "sub $1() { $i }"; $i++; } else { croak qq(bad enum member syntax: "$_") } } } } enum "INFIX, PREFIX=4, SUFFIX" ; print INFIX(), ":", PREFIX(), ":", SUFFIX(), "\n";

-- stefp -- check out TeXmacs wiki


In reply to perl6: Switch, another case (pun intended) for unscoped eval by stefp

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.