1

For example, could I write something like:

my $var = "A" || "B";

where ($var eq "A") and ($var eq "B") would both evaluate to true? If not, is there some way around this?

2
  • Why would you want that? What should print $var output in such case? Commented Apr 14, 2017 at 7:20
  • I think you want to use hash, so if ($var eq "A") should translate to if ($var{A}) Commented Apr 14, 2017 at 7:25

3 Answers 3

7

Are you looking for Quantum::Superpositions?

Update: An example.

#!/usr/bin/perl

use strict;
use warnings;
use feature 'say';

use Quantum::Superpositions;

my $var = any('A', 'B', 'C');

for ('A', 'B') {
  if ($var eq $_) {
    say "\$var equals $_";
  } else {
    say "\$var doesn't equal $_";
  }
}

Output:

$var equals A
$var equals B
$var doesn't equal C
Sign up to request clarification or add additional context in comments.

7 Comments

Of all the modules I've never found a practical use for, Quantum::Superpositions has always been my favorite.
I think you need to include an example. This is pretty weird stuff. :)
@simbabque: Good idea. Done :-)
Wow! I'd never noticed this module before. How bizarre. Good call for an answer though, but on the face of it I would use my $var = { A => 1, B => 1} and if ( $var->{$_} )
@DaveCross Interesting! Thanks for the example.
|
1

EDIT: From what I've gathered from your comments and previous question you have a string with chars and just want to check if one char is inside of it. Instead of constructing something like $var = "A" || "B" (which doesn't work) you can simply check if the needed char is in the string (with a simple regex):

$var = "AB";
if ($var =~ /A/) { # True
    # Calculations
}
if ($var =~ /B/) { # Also true
    # Other calculations
}

Comments

0

You cannot. It sounds like you misunderstand logical operator and bitwise operator.

| is bitwise operator, it works on bits.

|| is logical operator

From what you say, I presume you need logical operator, but be more specific.

You can get some help there: http://www.perlmonks.org/?node_id=301355

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.