Tricky has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Control flow revisited
by dragonchild (Archbishop) on Sep 05, 2003 at 16:33 UTC
    After reading a lot of the responses, as well as some of your other questions, I am coming to a very important conclusion. You are not really designing your code. You're throwing stuff together and getting very confused when it doesn't work.

    You really need to get away from the keyboard and put down on paper exactly the steps you want your code to achieve from the user's perspective. Treat your code as a black box and write down the actual specifications. Important questions you need answers to:

    • What inputs are expected?
    • Are these inputs user-specified? Hard-coded? Figured out on the fly?
    • Which inputs are mandatory? Which are optional?
    • How are the inputs specified?
    • What format(s), if any, will these inputs be in? What parsing capabilities already exist? (For example, HTML::Parser, XML::Parser, Spreadsheet::ParseExcel ...)
    • What outputs are expected?
    • Are these outputs user-specified? Hard-coded? Figured out on the fly?
    • Which outputs are mandatory? Which are optional?
    • What format(s), if any, will these outs be in? What templating capabilities already exist? (For example, HTML::Template, PDF::Template, Spreadsheet::WriteExcel ...)
    • How are the outputs specified?
    • (This is the big one) What am I expected to do?
    • What steps do I need to take to achieve my goal?
    • What error conditions should I be checking for?
    • If I encounter a specific error, what should I do with it? Should I
      • fail?
      • recover and continue?
      • write to a logfile?
      • display a message to the user?
      • do something else?
      • do some combination of the above choices?
    • (This is another big one) How am I going to validate that my implementation meets the above specifications?

    And, that's only a subset of the questions you need to ask. Remember - you are telling a being with a 0 IQ exactly what it needs to do. Unless you explicitly tell it, it won't know to do it. If you don't check for a situation, you cannot expect it to do so. So, if you're not sure of what you want to do, you have absolutely no way of telling the computer what to do.

    Programming, in my mind, should be 75% design and 25% coding. Both of those tasks should be 40% doing the task and 60% verifying you did the task correctly. So, if you do the numbers, you should really be spending your time as such:

    • Design/Specification - 30%
    • Design/Specification verification - 45%
    • Coding - 10%
    • Coding verification - 15%

    Yes, design is that important. If you have it right, correct coding is a breeze. If you don't have it at all, correct coding is impossible.

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      Cheers for the advice, dragonchild. Haven't seen the wood for the trees. So glad I visit the Monastery! I've set the guidelines down in stone (again). If I posted material to the monastery, would you folks be willing to critique?

      Regarding the coding problem I posted - it's now sorted! Time to get back to the drawing board... : )

Re: Control flow revisited
by Aragorn (Curate) on Sep 05, 2003 at 13:21 UTC
    Ok, you only want to do the search-and-replace if there is no word-spacing attribute in the HTML file. A way to do it is to create a variable $no_attr and set it to 1 if there's no word-spacing attribute in the file. The problem you have is that the file is in an array, and thus you have to iterate through it to determine if the attribute is present or not. A way to do it is to create a single string out of the array and check that string. Line 25 then becomes:
    if (join('', @htmlLines) !~ m/word-spacing:\s?[\d]+px/ig) { ... rest of the code ...
    Another way is to slurp the file into a single variable:
    $/ = undef; $html_file = <INFILE>
    And do the search/replace on this string, all in one go. This is left as an exercise ;-)

    Arjen

Re: Control flow revisited
by Abigail-II (Bishop) on Sep 05, 2003 at 12:44 UTC
    Why do you want to use a variable there? You aren't using the variable elsewhere. Why not just:
    if ($htmlLines [100] !~ m/word-spacing:\s?[\d]+px/ig)

    assuming that 100 is the line you want to match.

    Abigail

Why that's an error
by DentArthurDent (Monk) on Sep 05, 2003 at 15:57 UTC
    Just to be clear, the reason that that is an error is because the index into the array that you were using was not defined. One way to solve that would be to initialize the variable before the statement.