Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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:

( a ( a ( a ( a ( aa ) a ) a ) a ) a )
#! perl -slw use strict; sub recurse { print ">> '${ $_[ 0 ] }'"; if( length ${ $_[ 0 ] } ) { recurse( \ substr( ${ $_[ 0 ] }, 1, -1 ) ); ${ $_[ 0 ] } = " ( ${ $_[ 0 ] } ) "; } print "<< '${ $_[ 0 ] }'"; return; } my $str = 'aaaaaaaaaa'; recurse \$str; print $str; __END__ P:\test>junk >> 'aaaaaaaaaa' >> 'aaaaaaaa' >> 'aaaaaa' >> 'aaaa' >> 'aa' >> '' << '' << ' ( aa ) ' << ' ( aaaa ) ' << ' ( aaaaaa ) ' << ' ( aaaaa' << ' ( a ( aaaaaaaa ) a ) ' ( a ( aaaaaaaa ) a )

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.

In reply to Scalar refs, aliasing, and recursion weirdness. by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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







    Results (72 votes). Check out past polls.

    Notices?