Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Switch/case as a jump table with C-style fall-through

by Animator (Hermit)
on May 12, 2005 at 13:00 UTC ( #456370=note: print w/replies, xml ) Need Help??

in reply to Switch/case as a dispatch table with C-style fall-through

Some of my thoughts:

Not sure if it is useful, but how about calling the coderef with the value it triggerd? (as in, $case->("cat"); runs the coderef of the animal with the argument 'cat')

Here is a suggestion of which I'm really not sure: maybe it would be better to use a different name then 'new'? Pepole might expect a blessed reference and not a coderef... when you use new someone might think he should do something like $case->do_case(), so be really careful with how you name it...

And an identifier for the default code/value might be useful aswell, and/or maybe a continue/finalise block? (I did see some discussion about finalise before (but that was in another language), I leave it up to you to decide wheter or not that is useful)

A suggestion I was planning to make when I first looked at it was to allow a scalar instead a coderef too, but after taking a better look I realised this was not possible, since multiple keys can refer to the same code. (unless when there is a static-element added before the start of every key-block... which would mean you can just look at the one matched before, but this will make it more complex I guess)

update: typo :(

Replies are listed 'Best First'.
Re^2: Switch/case as a jump table with C-style fall-through
by Roy Johnson (Monsignor) on May 12, 2005 at 16:06 UTC
    I also wasn't sure it would be useful, but in the case of fall-through, I can see how it might come in handy to pass the matched string. That will go into the next revision.

    I'm not married to the name 'new' (and when I package it with other flavors of Case, it will certainly have a different name), but I didn't have a better name. I think it's just as legitimate to call a factory "new" as it is to call an OO constructor "new". The users are expected to read the documentation.

    I left out a default identifier because I didn't want to have a string that might conflict with an alternative. But it occurred to me that a call to a subroutine that returns an empty list could be tucked in there and be absolutely transparent. That will also be in the next revision.

    I don't know what a continue/finalise block should do here. Could you explain?

    Thanks for your input.

    Caution: Contents may have been coded under pressure.

      I see now that I basiclly took it from the wrong concept... (what I initially though about had more to do with something like eval then with switch)

      I have no idea wheter or not it would be a good idea to implement what I was thinking about... I'll continue explaining and then you can make up your mind... I was thinking about creating a 'handler' that is run at the end if one (or more) case-statements matched...

      I'm afraid I didn't explain to well and that it might be easier to explain it with an example/code, so that's what I will do now:

      Assume that at the end of each case there is some default action that should be taken (update a hash with a list of matches or something). With the current implementation there are two ways to accomplish this:

      a) adding the code it in each code-ref (by calling another subroutine)
      b) using something like $case->("something") and some_other_code; (although in this case it would depend on the return value, but let's assume it returns a true value when the code-ref was run)

      Both of these have some disadvantages, the first one has the disadvantage of the duplicate function-call and the second depends on the return value

      What my suggestion was about was to be able to set up something like 'FINAL' (or 'CONTINUE') => sub { ... } which will run the code (add it to a hash or whatever) if a case returned true

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://456370]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2023-02-06 07:05 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (33 votes). Check out past polls.