It's a far to general question to be answered easily, could you show us some code?
> # I leave the if statements but then when you call a sub you can't really tell what it will return. As it can vary depending on what if's it might match.
I would suggest to write a wrapper¹ to this legacy code, controlling the state of these global flags
sub wrap_render { local $x=shift; local $y=shift; if ($var) { return "your change"; } else return render(); } }
like this you have full control about this "alien" function.
As a general principle I prefer code to be easily understandable and avoid to clutter it with if statements which eventually turns the flow into a labyrinth. Furthermore I try to localize global vars as tight as possible.
So instead of something like
sub render_a { print "<a "; print "style='$css'" if $global_css_flag; print ">" }
I would write something like
sub render_a { my $style=get_css(); print "<a $style>"; } { my $css_flag; sub get_css { return "style='$css'" if $css_flag; } sub css_flag { if (@_) {$css_flag=shift} else {$css_flag} } # gette +r-setter }
(untested)
Cheers Rolf
UPDATE: added checking of new flag $var.
¹) in perl you can give the wrapper the same function name like the wrapped function you're calling from within! Just ask if you wanna go this way!
In reply to Re: Best way to handle small changes to subroutines
by LanX
in thread Best way to handle small changes to subroutines
by code4pay
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |