With tr///
, I can do this:
my $str = 'xABCz';
$str ~~ tr:c/ABC//;
say "-->{$str}<--";
--output:--
-->ABC<--
But with TR///
, which creates a new string, there doesn't seem to be a way to apply it directly to a string:
my $str = 'xABCz';
$str ~~ tr:c/ABC//;
my $new_str = $str ~~ TR:c:d/ABC//;
say "-->{$new_str}<--";
--output:--
-->True<--
I can use with
to set $_
to $str
:
my $str = 'xABCz';
with $str { # now $_ refers to $str
my $new_str = TR:c:d/ABC//; # TR/// is called on $_.
say "-->{$new_str}<--";
}
--output:--
-->ABC<--
...or even:
my $str = 'xABCz';
my $new_str = TR:c:d/ABC// with $str;
say "-->{$new_str}<--";
--output:--
-->ABC<--
In perl, I would write:
my $str = 'xABCz';
my $new_str = $str =~ tr/ABC//rcd; #r - new_string, c - complement, d - delete
say $new_str;
-output:--
ABC
Str.trans
should have the same effect non-destructively.
my $new_str = $str.trans("ABC" => "")