Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl6/rakudo: How can I change the data-type of a variable?

#!perl6
use v6;

my $m = 70;
my $n = 30;

( $m div $n ).say;

The first examples works, but the second doesn't. I suppose it's because in the second example the variable-values are strings. If my guess is right, how could I change the string-variables to integer-variables?

#!perl6
use v6;

my $m = '70';
my $n = '30';

( $m div $n ).say;


# No applicable candidates found to dispatch to for 'infix:<div>'. 
# Available candidates are:
# :(Int $a, Int $b)

#   in main program body at line 7:./perl5.pl
like image 431
sid_com Avatar asked Dec 12 '22 15:12

sid_com


1 Answers

You can always manually cast to Int

( $m.Int div $n.Int ).say;

Actually I would have hoped that prefix:<+> would work as in

( +$m div +$n ).say;

But it just "Num"ifies and the sig requires "Int", I am not sure if it should be this way or not.

UPDATE: +$m now works.

like image 135
Pat Avatar answered Jan 20 '23 12:01

Pat