http://qs1969.pair.com?node_id=428261


in reply to Re: Scalar refs, aliasing, and recursion weirdness.
in thread Scalar refs, aliasing, and recursion weirdness.

Thanks. I think you are right regarding the re-use of a modified lvalue ref.

Whilst your code achieves the notionally "desired" output of the testcode I posted, as I said, this is a simplification.

The real application could be recursing into multiple substrings of the parameter at each level. Where and when the recursion occurs is dependant upon the content of the (sub)string passed and cannot be easily codified into a regex. For various reasons I wish to avoid using the regex engine also.

That said, I may not have those choices now.


Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.
  • Comment on Re^2: Scalar refs, aliasing, and recursion weirdness.

Replies are listed 'Best First'.
Re^3: Scalar refs, aliasing, and recursion weirdness.
by sleepingsquirrel (Hermit) on Feb 05, 2005 at 01:14 UTC
    Still probably not what you are looking for (i.e. no refs), but closer maybe? (at least no regex)
    #!/usr/bin/perl -slw use strict; my $str = 'aaaaaaaaaa'; print recur($str); sub recur { my $s = shift; return "" if length($s)==0; substr($s,1,-1) = recur(substr($s,1,-1)); return "($s)"; }


    -- All code is 100% tested and functional unless otherwise noted.
      Still probably not what you are looking for (i.e. no refs)

      The refs were only a half-arsed attempt to get thing to work when I suspected that the @_ aliasing was responsible for "undo" my changes--a total wrong guess!

      And, your post has moved me along considerably. Your insight of using the same substr as an lvalue into which to assign the return from the recusion--with the same parameters as are used to supply the substring to that level of recursion is (I think) the solution.

      I've put that into the real code and it gets me closer to the desired result. At this stage, the rest of the problem appears to down to my selecting the correct substring--ie. a completely different part of the problem.

      So thankyou. Very much.


      Examine what is said, not who speaks.
      Silence betokens consent.
      Love the truth but pardon error.