How can I split a Perl string only on the last occurrence of the separator?
You could use pattern matching instead of split()
:
my ($a, $b) = $str =~ /(.*):(.*)/;
The first group captures everything up to the last occurence of ':'
greedily, and the second group captures the rest.
In case the ':'
is not present in the string, Perl is clever enough to detect that and fail the match without any backtracking.
split(/:([^:]+)$/, $str)