Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Scalar refs, aliasing, and recursion weirdness.

by ikegami (Pope)
on Feb 04, 2005 at 22:43 UTC ( #428226=note: print w/replies, xml ) Need Help??


in reply to Scalar refs, aliasing, and recursion weirdness.

I don't know why, but on a hunch, I tried this change:

#! perl -slw use strict; sub recurse { print ">> '${ $_[ 0 ] }'"; if( length ${ $_[ 0 ] } ) { #recurse( \ substr( ${ $_[ 0 ] }, 1, -1 ) ); #old recurse( \( my $boo = substr( ${ $_[ 0 ] }, 1, -1 ) ) ); #new ${ $_[ 0 ] } = " ( ${ $_[ 0 ] } ) "; } print "<< '${ $_[ 0 ] }'"; return; } my $str = 'aaaaaaaaaa'; recurse \$str; print $str;

and it worked. I leave it up to you to explain it (cause I'm going home!)

I'm don't think this is the output you want, because it would be a very convoluted way of getting it. But you didn't specify what you wanted.

Replies are listed 'Best First'.
Re^2: Scalar refs, aliasing, and recursion weirdness.
by BrowserUk (Pope) on Feb 04, 2005 at 22:54 UTC
    and it worked.

    Actually not--unless your results are different to mine?

    It only retains the last change and I want to retain the accumulated changes.

    P:\test>junk >> 'aaaaaaaaaa' >> 'aaaaaaaa' >> 'aaaaaa' >> 'aaaa' >> 'aa' >> '' << '' << ' ( aa ) ' << ' ( aaaa ) ' << ' ( aaaaaa ) ' << ' ( aaaaaaaa ) ' << ' ( aaaaaaaaaa ) ' ( aaaaaaaaaa )

    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.
      Maybe you want
      ${ $_[ 0 ] } = " ( $boo ) ";
      or

      if( length ${ $_[ 0 ] } > 2) { my ($pre, $mid, $post) = /^(.)(.*)(.)$/; recurse( $mid ); ${ $_[ 0 ] } = "$pre ( $mid ) $post"; }

      Just guessing, cause you didn't specify what output you want.

        I've updated the OP to clarify my expectations.

        The whole point is to modify the referent in-place. The selection for the substring reference used by the recursion is far more complicated in the real situation.

        The point of my question is not really "How to get this output", but more "Why do changes that get made during recursion get unwound, when the entity being modified is (relatively speaking) static."


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://428226]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2021-10-18 05:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (72 votes). Check out past polls.

    Notices?