Since the matter hinges on knowing what the first and last elements of the string are, I'd be inclined to start by splitting the string into an array, which makes it easy to treat the first and last directly. A capturing split would do it well enough:
#!/usr/bin/perl
use strict;
use warnings;
my %rules = (A => 1,B => 1,C => 0,D => 1);
while (<DATA>) {
chomp;
my @chunks = split( /([ABCD])/ );
my $frst_add = my $last_add = 0;
if ( $chunks[1] eq 'D' ) {
$frst_add = $chunks[0] * $rules{D};
shift @chunks;
shift @chunks;
}
if ( $chunks[-1] eq 'D' ) {
$last_add = $chunks[-2] * $rules{D};
pop @chunks;
pop @chunks;
}
my $s = 0;
for ( my $i = 1; $i < @chunks; $i += 2 ) {
$s += $chunks[$i-1] * $rules{$chunks[$i]};
}
my $s1 = $s + $frst_add;
my $s2 = $s + $last_add;
$s += $frst_add + $last_add;
printf "%s :\ts= %2d s1= %2d s2= %2d\n", $_, $s, $s1, $s2;
}
__DATA__
1A2B3C3B1D
2D40A2C1B4D
2D40A2C1B
(Of course, having written this as a stand-alone script, I think it would be better as a subroutine, like what AM did above.)