note
MarkusLaker
perlfunc's entry on <code>substr</code> says:
<p>
<i>If the lvalue returned by substr is used after the EXPR is changed in any way, the behaviour may not be as expected and is subject to change. This caveat includes code such as <code>print(substr($foo,$a,$b)=$bar)</code> or <code>(substr($foo,$a,$b)=$bar)=$fud</code> (where <code>$foo</code> is changed via the substring assignment, and then the substr is used again), or where a <code>substr()</code> is aliased via a <code>foreach</code> loop or passed as a parameter or a reference to it is taken and then the alias, parameter, or deref'd reference either is used after the original EXPR has been changed or is assigned to and then used a second time.</i>
<p>
I had to read that last sentence several times to understand it, but I think it covers what your code does: you've taken a reference to a substr, changed the referee string, taken a reference to a substring of that referee, and changed the referee of <i>that</i>. Don't do that.
<p>
Here's some code that does what you want:
<p>
<code>
#!/usr/bin/perl -l
use warnings;
use strict;
sub recurse($);
sub recurse($) {
my $a = $_[0];
print ">> '$a'";
$a =~ s/ ^ (.) (.+) (.) $ / join '', $1, recurse $2, $3 /ex;
print "<< '$a'";
" ( $a ) ";
}
print "Result: ", recurse 'aaaaaaaaaa';
</code>
<p>
Markus
<p>
<b>Update:</b> diotalevi pointed out that my description of the problem with the OP's code was wrong. I've fixed it.
428220
428220