Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

(ichimunki) Re x 3: Thoughts on Perl6 - Love it? Hate it?

by ichimunki (Priest)
on Jan 10, 2002 at 23:17 UTC ( #137824=note: print w/ replies, xml ) Need Help??


in reply to Re: (ihcimunki) Re: Thoughts on Perl6 - Love it? Hate it?
in thread Thoughts on Perl6 - Love it? Hate it?

Because FOO, BAR, and BAZ are not conditions, they are static labels. Is there a way to get them to behave as ranges or any sort of boolean? If so, I still don't have a problem with switch, but I would be glad to have another trick up my sleeve. :)


Comment on (ichimunki) Re x 3: Thoughts on Perl6 - Love it? Hate it?
Re: (ichimunki) Re x 3: Thoughts on Perl6 - Love it? Hate it?
by dragonchild (Archbishop) on Jan 10, 2002 at 23:30 UTC
    Yah, there is.
    sub Switcheroo { my @conditions = @_; if ($conditions[0] < 10) { return 'FOO'; } else { return 'BAR'; } }
    The point is to encapsulate the strategy of determining which direction to go. That's what a switch statement does. This is, basically, a Switch object. (An example of a Strategy pattern, for those following along at home.)

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

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      Your example methodology adds not one, but two layers of indirection to what is otherwise a straightforward question-- and gets none of the fall through capability of the typical switch statement at the same time. It is therefore less than optimal in situations where switch is designed to be most used.

      The one place your indirect method absolutely shines is when you want to set up a mutating hash of anonymous subs (the dispatch table essentially). You keep your condition tree static perhaps (or the condition tree could be just another dispatch (or dispatches!) in the dispatch table). Then you can swap different subs in an out for the same results/conditions labels. Although I'd have to wonder if you can do this for any non-trivial, non-proof of concept application and maintain your sanity or the application later. *grin*
        No, I don't have the fall-through capability, natively. One would have to have the functions themselves call each other to gain fall-through. (If 'A' would fall through to 'B', then A() does a return B(@_); at the end.) This is somewhat counter-intuitive and very tightly coupled, but it does have benefits.

        The primary benefit I see, apart from the dynamic nature of it, is that I can pass args to my switch clauses. In addition, I've separated out my switch clauses from one another, which means that they can be arbitrarily long and not go over-long within the same function.

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

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (12)
As of 2014-09-02 07:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (20 votes), past polls