Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

perl6: Switch, another case (pun intended) for unscoped eval

by stefp (Vicar)
on Jul 12, 2002 at 17:23 UTC ( #181349=perlmeditation: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
Re: perl6: Switch, another case (pun indented) for scopeless eval
by Elian (Parson) on Jul 12, 2002 at 18:01 UTC
    Switch is implemented with source filters, so it doesn't have the same set of issuee as you're facing. (as it's a compile-time source transform rather than a runtime twiddling of things) You might want to take a look at source filters--they're generally more appropriate for new syntax than runtime options.

    Which doesn't mean scopeless evals are bad, of course. :)

      Certainly enums should be programmed as a filter since stacking filters is allowed.

      After filtering by Switch, my program becomes what is listed below and the subroutine Switch::case is called with my regexp as parameter. So, my bad, this seems to be a case for unscoped sub instead of unscoped eval. BTW: I am peeking at parrot and is positively impressed.

      use Carp; sub enum { my @enum = split /,/, $_[0]; my $i = 0; for ( @enum ) { S_W_I_T_C_H: while (1) { local $::_S_W_I_T_C_H; Switch::switch ( +$_); if (Switch::case qr/([A-Z]+\d*)(?:\s*=\s*(\d)+)?/i){ while (1) +{ m/([A-Z]+\d*)(?:\s*=\s*(\d)+)?/i; no strict; $i = $2 if defined $2; eval "sub $1() { $i }"; $i++; ;last S_W_I_T_C_H } continue { goto C_A_S_E_1 } last S_W_I_T_C_H; +C_A_S_E_1: } else { croak qq(bad enum member syntax: "$_") } }continue {last} } } enum "INFIX, PREFIX=4, SUFFIX" ; print INFIX(), ":", PREFIX(), ":", SUFFIX(), "\n";

      -- stefp -- check out TeXmacs wiki

Re: perl6: Switch, another case (pun indented) for scopeless eval
by Anonymous Monk on Jul 12, 2002 at 18:31 UTC
    FYI: since you welcome a pointer. enum

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://181349]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2021-02-27 03:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?