MZSanford has asked for the wisdom of the Perl Monks concerning the following question:

    Ok, here goes. Here is some backround, but if you don't need it, skip to the next paragraph. I have been working on a bit of code to save coders from them selves. What i am doing is adding failure notification to a ton of code which otherwise has none. The idea is to wrap then entire process in an eval {} and then do error reporting based on that. While this is no huge task, i am also working on a restart logic. By this i mean, i am getting command line arguments, and if -restart is specified with a step name, the code is to go to that step. I would normally do this via goto, or write the code as an array of functions, but there is a snag. All of this code is being made to look outwardly (and somewhat inwardly, to make training Tcl developer easier (says Tcl Library author)) the same as a Tcl library written by a colleage.
    And the problem. I want to let developers specify new "step's" in the code using the syntax :
restartStep step_name1 { ## code here } restartStep step_name2 { ## code here }
    I also need to list the step names in my usage message. I am currently using a solution, which is, in my opinion, clumsy. I am currently using source filtering to find the step names, make each one a sub-routine in my own sub-name space (which, i guess i could put into Safe for a testing mode), and save aa array of step names in order (for usage and such).
is there a way to get this syntax without Filter ? I have looked at prototypes, but been unable to get this to work. What i really am wanting here is a syntax the same as sub, but from what i see of the source, there is no real way to do that. am i missing something, or is this correct ? i figured if anyone would have found a way to do this, it would be a fellow Monk out there.
can't sleep clowns will eat me
-- MZSanford

Replies are listed 'Best First'.
Re: syntactic sugar, hemlock, and other such fun
by Masem (Monsignor) on Aug 30, 2001 at 15:17 UTC
    One possible option:
    sub step_name1 { print "stuff\n"; } sub step_name2 { print "more stuff\n";} sub restartStep ($) { my $code = shift; # Optional: test for existance of Package::"$code" here &$code; } restartStep "step_name1"; restartStep "step_name2";

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    It's not what you know, but knowing how to find it if you don't know that's important

      Please note that Masem's solution will NOT run under strict 'refs'. I personally consider this less-than-optimal, though it is a perfectly good solution.

      It sounds like you're trying to do some sort of dispatch table. So, just use a dispatch table.

      sub foo { print "Foo!\n"; } sub bar { print "Bar!\n"; } my %dispatch = ( foo => \&foo, bar => \&bar, ); for my $key (keys %dispatch) { print "$key ... "; &{$dispatch{$key}}; }
      This has an additional benefit of not mispelling anything.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Vote paco for President!

Re: syntactic sugar, hemlock, and other such fun
by John M. Dlugosz (Monsignor) on Aug 30, 2001 at 20:07 UTC
    As I read it, the point isn't figuring out how to make it work, but rather how to alter your source syntax.

    The problem with prototypes is that the implicit sub thing only works for the first argument. So if you wanted to write:

    step { code_goes_here; } "stepname";
    but I don't think that's nice. How about a few more "keywords" in your syntax?
    step "stepname", sub { code_goes_here; }
    You can use a function with a prototype to make sub into any other word you want. You can use overloading to change the comma to something else.

    Interesting problem.

    —John