Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

=~ doesn't work with Quantum::Superpositions

by diotalevi (Canon)
on Apr 20, 2003 at 15:24 UTC ( #251828=perlquestion: print w/replies, xml ) Need Help??
diotalevi has asked for the wisdom of the Perl Monks concerning the following question:

I tried matching against a disjunction and got the wrong results. Where is the problem - my understanding or in Q::S?

use Quantum::Superpositions; $\ = $/; # Create a disjunction of 'a' and 'b'. $dis = any( 'a', 'b' ); print "=~: ",$dis =~ /^b$/; # false print "eq: ",$dis eq 'b'; # true

Replies are listed 'Best First'.
Re: =~ doesn't work with Quantum::Superpositions
by DrManhattan (Chaplain) on Apr 20, 2003 at 15:57 UTC
    Looks like eq is overloaded, but =~ is not. When you make a regex comparison, you're just getting a regex match against the stringified object (in this case "any(a,b)") on the LHS.
    use Quantum::Superpositions; # Create a disjunction of 'a' and 'b'. $dis = any( 'a', 'b' ); $dis =~ /.*/ && print $&;
    Prints "any(a,b)"


Re: =~ doesn't work with Quantum::Superpositions
by Limbic~Region (Chancellor) on Apr 20, 2003 at 16:30 UTC
    I could not find a single example of the binding operator anywhere in the documentation for Quantum::Superpositions. I did however find the following code interesting:

    #!/usr/bin/perl use Quantum::Superpositions; $\ = $/; # Create a disjunction of 'a' and 'b'. $dis = any( 'a', 'b' ); print $dis; print "=~: ",$dis =~ /\bb\b/; # true print "eq: ",$dis eq 'b'; # true

    I do believe Perl 6's built in any will work with the binding operator. I also took a peak under the hood of the module itself. I could not find a binding operator listed:

    q{<} => sub { qblop(swap(@_), sub { $_[0] < $_[1] })}, q{<=>} => sub { qblop(swap(@_), sub { $_[0] <=> $_[1] })}, ... all kinds of other tests q{~} => sub { $_[0]->quop(sub { ~$_[0] })},

    Happy hunting!

    Cheers - L~R

    Update: I guess I spent too long digging around under the hood as Matt beat me to the punch.

    Update 2: Chmrr reminded me that ~ is bitwise negation and that the binding operator can't be overloaded - see perldoc overload - Chmrr++

      As to the perl 6 stuff, BTW, there is a binding operator, :=, but it's nothing like =~; it binds one variable to be an alias of another. =~ is becomming the smart-match operator, ~~, which will do even more then =~, becomming polymorphic on both sides, doing things like array intersection.

      In any case, pretty much all operators are overloadable in perl6. However, smart-match is probably smart enough to call the approprate string-/num-/bool-/etc-rafication overloads, as approprate.

      Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://251828]
Approved by DrManhattan
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2018-01-24 00:20 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (254 votes). Check out past polls.