You are right be worried, your example code is just a bit to complex to make it evident at first glance.
There is no logical reason why nested calls of the same function (i.e. recursions) should act differently
to nested calls of different functions.
see updated code, especially the second paragraph contrasting the bug.
UPDATE:
to be sure to avoid any side effects from eval within the debugger here a standalone file for testing:
use warnings;
use strict;
use 5.10.0;
my $x;
sub delchar { $x =~ s/(\w)// ? $1 . delchar() . $1 : "x" }
$x='abc';
say delchar(); # => "cccxccc"
sub del1 { $x =~ s/(\w)// ? $1 . del2() . $1 : "x" }
sub del2 { $x =~ s/(\w)// ? $1 . del3() . $1 : "x" }
sub del3 { $x =~ s/(\w)// ? $1 . del4() . $1 : "x" }
sub del4 { $x =~ s/(\w)// ? $1 . del5() . $1 : "x" }
$x='abc';
say del1(); # => "abcxcba"
UPDATE:
Best practice is to copy captures like $1 ASAP! (not only in recursions)
DB<105> sub delchar { my $m; $m = $1 if $x =~ s/(\w)//; $m ? $m .
+delchar() . $m : "-" }
=> 0
DB<106> $x='abc'; delchar()
=> "abc-cba"
DB<107> sub delchar { local $m; $m = $1 if $x =~ s/(\w)//; $m ? $m
+ . delchar() . $m : "-" }
=> 0
DB<108> $x='abc'; delchar()
=> "abc-cba"
-
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 How to display code and escape characters
are good places to start.
|