Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Re: Re: Re: Perl Idioms Explained: && and || "Short Circuit" operators

by kelan (Deacon)
on Oct 23, 2003 at 14:06 UTC ( [id://301588]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Perl Idioms Explained: && and || "Short Circuit" operators
in thread Perl Idioms Explained - && and || "Short Circuit" operators

Indeed if having an execution of O(1) is switch's primary benefit, that will not be present in Perl6's switch, except perhaps in primitive uses. C's switches must have constant case labels to facilitate the chicanery it uses for producing O(1) behavior. Perl6 will not enforce constants as the "labels". From what I understand, you'll be able to use any Perl expression you wish as a label. Therefore, each one will need to be evaluated, at runtime, to find the correct one. I think in this case, the benefit to the switch statement for Perl would be its conciseness over using chained if/elses.

kelan


Perl6 Grammar Student

  • Comment on Re: Re: Re: Re: Perl Idioms Explained: && and || "Short Circuit" operators

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Perl Idioms Explained: && and || "Short Circuit" operators
by hardburn (Abbot) on Oct 23, 2003 at 16:28 UTC

    Hrm, that's kinda unfortunate. A C-style switch is always a tradeoff, flexibility wise, but the tradeoff can be big if you have a large number of cases. I supose if we're lucky, perl6 will optimize switches that use constant expressions into a jump table, though I suspect such a feature wouldn't be out in the first version of perl6--time enough for optimizations later.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    :(){ :|:&};:

    Note: All code is untested, unless otherwise stated

Re: Re: Re: Re: Re: Perl Idioms Explained: && and || "Short Circuit" operators
by BrowserUk (Patriarch) on Oct 23, 2003 at 19:46 UTC

    I look forward to the power of the P6 Given/When construct immensely, but I still hanker for the the limited but oh-so-useful and efficient switch/case construct. There are so many occasions when this simple integer-based dispatch mechanism is exactly all that is needed, that I think it would be worth having, in addition to the much more powerful, but equally, much less efficient Given/When construct.

    I'm aware of theDamian's switch module, but it attempts to be much more than a simple switch construct, coming much closer to the given/when flexibility. The downside of that it that it is much less efficient for the predominant 'simple case'. I've also had problems with it getting confused.

    Hash based dispatch tables are okay and pretty efficient, but the need to put the selected code into either a sub or runtime eval makes it less useful for things where the selected code is minimal and speed important. Calling a sub to increment or decrement a count is high overhead if your scanning long strings and maintaining counts as you go.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-16 04:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found