in reply to Re: Modifying a parameter to a recursive function
in thread Modifying a parameter to a recursive function

While your points are definitely good advice, I'm not sure that's what he's asking about. I think the OP is asking about modifying the parameters to the subroutine in-place. For example:
@a = (1..10); sub x { foreach (@_) { $_++; } } x(@a); print @a; #prints 234567891011
So in principle, you can modify a subroutine's arguments using the default variable $_ inside a foreach loop. Question is, why isn't it working inside his recursive function?

Replies are listed 'Best First'.
Re^3: Modifying a parameter to a recursive function
by GrandFather (Saint) on Apr 08, 2009 at 21:42 UTC

    I don't think that is really the OP's problem. What you suggest can be checked trivially by:

    use strict; use warnings; my @array = (1 .. 8); print "Before: @array\n"; nastyModifyParamsSub (@array); print "After: @array\n"; sub nastyModifyParamsSub { for my $element (@_) { ++$element; } }

    Prints:

    Before: 1 2 3 4 5 6 7 8 After: 2 3 4 5 6 7 8 9

    Which shows that modifying the parameters is possible and that there is nothing magical about requiring $_ to do it. The following though shows one way that OP's code could be going awry:

    use strict; use warnings; my @array = (1 .. 8); print "Before: @array\n"; nastyModifyParamsSub (@array); print "After: @array\n"; sub nastyModifyParamsSub { for (@_) { ++$_; clobberDefVar (); } } sub clobberDefVar { $_ = 0; }

    Prints:

    Before: 1 2 3 4 5 6 7 8 After: 0 0 0 0 0 0 0 0

    The bottom line though to that the OP's description is an fuzzy unclear thing and there is too much code and no data at all.


    True laziness is hard work

      I'm trying to see it your way, but I don't see where you think there's an unlocalized $_.

      tag and content_list could theoretically clobber $_, but you'd figure that subsequent method calls would fail if they did.