The immediate 'problem' is that the for loop variable ($_ in your case) is an alias to each element in turn of the list being iterated over. It really isn't the same $_ that is used outside the loop. The same thing happens with $_ in map and grep. If you really want to retain the last processed value in a for loop you need to copy it to another variable explicitly:
sub traverse { my $result; for my element (@_) { ... $result = 'my interesting value to be retained on the last ite +ration'; } return $result; }
You really, really, really ought to use a variable instead of the default variable for your for loop variable btw. Using the default variable for more than a couple of lines and expecting it not to change by magic is just asking for trouble!
Oh, and you should use the three parameter version of open and lexical file handles:
open my $outputFile, '>', $output or die "Failed to create $output: $! +\n";
In reply to Re: Modifying a parameter to a recursive function
by GrandFather
in thread Modifying a parameter to a recursive function
by CoDeReBeL
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |