5

If I try to declare a Mix with Boolean components:

my $mix= (True => 0.3, False => 0.7).Mix;
dd $mix; # OUTPUT: «Mix $mix = ("True"=>0.3,"False"=>0.7).Mix␤»

They use Pair syntax, which quotes automatically those bare identifiers. In order to avoid that, you either have to define the Pairs explicitly via Pair.new, or else use the fully qualified name.

my $mix= (Bool::True => 0.3, Bool::False => 0.7).Mix;

Is there any other way of doing that? A simpler way maybe?

jjmerelo
  • 22,578
  • 8
  • 40
  • 86

2 Answers2

7

You can use anything that isn't seen as a bare-word.

Fully qualified names work.

Bool::True => 1

The reason they work is bare-words don't have :: in them.
So you can just prepend :: as well.

::True => 1

You can use ::(…)

::(True) => 1

::('True') => 1
::< True > => 1

You can also use () around True.

(True) => 1

You could declare it backwards and use .antipair

( 1 => True ).antipair

( :foo ).antipair # (Bool::True) => 'foo'

If you don't mind getting a sequence you can use .invert, or .antipairs

# Seq.new( Bool::True => 1 )
( 1 => True ).invert 
( 1 => True ).antipairs

# Seq.new( Bool::True => 1, Bool::False => 2 )
( 1 => True, 2 => False ).invert
( 1 => True, 2 => False ).antipairs

If True was a subroutine instead of a term, you could append ()

sub True ( --> True ){}

True() => 1

Then there is using Pair.new.

Pair.new( True, 1 )
Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129
5

Using parens as in (True) => 0.3 or the null pseudo-package as in ::True => 0.3 would be another option.

Christoph
  • 164,997
  • 36
  • 182
  • 240