Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

  • Step 1: Learn to write tests in Perl and create a separate test for every function that you plan to modify. Make certain that all tests pass 100% before proceeding. [ Contextual note: I add this because, when making changes to production code, you can avoid the fear of breaking things by having comprehensive testing available to run automated regression tests. Once you get used to doing this you will lose your fear of refactoring production code.]
  • Step 2: Change the offending function names by appending something to them. So sub print_l becomes sub print_l_old. Change the 'print' statements to 'return' statements. Rerun your tests. Everything should break (that's okay for now).
  • Step 3: Create new functions with the old function names and have them call the old functions like this:
    use strict; sub print_l_old { return qq|the world is a great place|; } sub print_l{ print print_l_old() . qq| to be.|; } print_l();
The idea of a function is normally to return something. In other languages there is a way to notify the user whether or not a function was supposed to be used to return anything (like declaring it void, etc.). There is not anything like this in Perl (that I know of) so my best practice is to almost always write functions to return something, even if that something is only undef. Whatever solution you come up with, I would add it to your coding standards. I added this section and all this help about refactoring because I think the code you have posted probably needs to be rewritten. There are good, safe ways to do that using Perl utilities.

Update: Oops, I forgot to mention that your tests will break if you add new stuff like  qq| to be.|. The right approach should be to just call the old functions in the new without appending anything and rerun your tests. They should all pass. Then rewrite your tests to match your next version's changes. You should be using a version control system, so you should be able to easily differentiate versions and tag releases along with associated tests.

Celebrate Intellectual Diversity


In reply to Re: Collect prints to a string by InfiniteSilence
in thread Collect prints to a string by vit

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



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-23 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found