in reply to Perl match to cut off after second occurrence

Try this:

(my $dir = $0) =~ s/([^\\]+\\([^\\]+\\)?)?.*$SC/$1/;