> Any thoughts?

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.