|Syntactic Confectionery Delight|
Scalar refs, aliasing, and recursion weirdness.by BrowserUk (Pope)
|on Feb 04, 2005 at 22:25 UTC||Need Help??|
BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:
The following is a gross simplification of a piece of code that almost works--but not quite.
The routine (recurse()) receives a reference to a scalar, inspects it for a condition, and recurses, passing a reference to a substring of it's own parameter, if that condition is met.
When the recursion unwinds, it adds it's modifications to the referent of it's parameter (one or more substrings of which may have been modified earlier in the recusion).
The trace shown below shows that at each level of recurion, the desired modifications are made, but, despite everything being done through aliases and references, the changes are being 'undone' as the recursion unwinds--but not completely. The last change made persists!?
Of particular interest is the value (that should be) returned from the second last level of recursion, which appears to be truncated some how?
Update: ikegami points out that I did not make clear what output I was expecting.
That would be the accumulated changes to the underlying referent. Ie. The last line of output should be:
Can anyone explain what is going on here--or better--make it work?
Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.