While all the Java examples work in Groovy, too, you can also use the toInteger() method that Groovy adds to all CharSequences. It throws a NumberFormatException if the string is not a number.
The function StrToInt will raise an exception of type EConvertError if the string is not a proper representation of an integer.
my $i = $s + 0;
Perl automatically converts numbers to strings, and strings to numbers, whenever required. This means the addition of 0 is strictly superfluous, it only indicates programmer intent.
leti = match s.parse::<i32>() {
Ok(i) => i,
Err(_e) => -1,
};
s is parsed to 32-bits signed integer here (change number type if needed). -1 is used here as a fallback value, but any error handling instructions can be used.