Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Scalar refs, aliasing, and recursion weirdness.

by !1 (Hermit)
on Feb 04, 2005 at 23:18 UTC ( #428239=note: print w/replies, xml ) Need Help??


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

Rather interesting behavior. If you merely use @_'s magic aliasing, the results seem even stranger.

#! perl -slw use strict; sub recurse { print \$_[0]; print ">> '$_[ 0 ]'"; if( length $_[ 0 ] ) { recurse( substr( $_[ 0 ] , 1, -1 ) ); $_[0] = " ( $_[0] ) "; } print "<< '$_[0]'"; return; } my $str = 'abcdefghij'; recurse $str; print $str; __END__ SCALAR(0x8126a70) >> 'abcdefghij' LVALUE(0x8126bc0) >> 'bcdefghi' SCALAR(0x812a454) >> 'cdefgh' SCALAR(0x812a520) >> 'defg' SCALAR(0x812a5ec) >> 'ef' SCALAR(0x812a6b8) >> '' << '' << ' (' << ' ( d' << ' ( c (' << ' ( b ( c' << ' ( a ( b ( c ( ) d ) ( ) ef ) ghij ) ' ( a ( b ( c ( ) d ) ( ) ef ) ghij )

Perhaps this is a bug in the magic lvalue-ness of substr? It doesn't seem to be verifying the length of an lvalue on an lvalue, thus the shifting of the terms that appear on the right.

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

    I think you may have hit the nail on the head.

    It would appear that if you modify an lvalue ref, it gets converted to a normal scalar before the modification.

    Which is annoying and counter-intuative (to me anyway), but maybe work that would be involved in allowing the underlying scalar to be modified through a substring of an lvalue ref is too complicated?

    Maybe I will have to code my own lvalue-ref-on-steroids to achieve my purpose?


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

      Even better example:

      #! perl -slw use strict; sub recurse { print ">> '${ $_[ 0 ] }'"; (my $depth=$_[1])--; if ($depth) { recurse( \ substr( ${ $_[ 0 ] }, 1, -1 ), $depth ); } else { ${$_[0]} = "XX"; } print "<< '${ $_[ 0 ] }'"; return; } my $str = 'abcdefghi'; recurse \$str, 3; print $str; recurse \$str, 2; print $str; __END__ >> 'abcdefghi' >> 'bcdefgh' >> 'cdefg' << 'XX' << 'bcdefgh' << 'abcdefghi' abcdefghi >> 'abcdefghi' >> 'bcdefgh' << 'XXi' << 'aXXi' aXXi

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (1)
As of 2021-07-30 16:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?