http://qs1969.pair.com?node_id=949539


in reply to Re^2: Matching and combining two text files
in thread Matching and combining two text files

hahahaha, <--- that's real laughter, not nervous, I look like a slump and need to play it off laughter. ;-) OK, so, I hardly ever use any of the types of syntax you do. I'm finding this a lot. I guess it's because I learned from the first edition books, and never have learned much about new ways, for instance,

open(IN, $file) || die $!

is the only way I know how to open a file, and have never bothered using a different method, just as I have never used

next if

and I've always been taught that nice clean code, always has all variables declared before hand, never in the middle of the program. So, basically, my "nastiness" of the code you gave me :p, was to figure out what you were doing, where I didn't understand. I have sort of been thrown into the big leagues whether I belong there or not, so I guess I better pick up a bat...

So, if I may, please help me understand why you're using the next if, instead of regular if's nested as I did, why nesting if statements is a bad idea, and what do you mean about slurping up files? I know I'm probably going to lose a million XP and get slammed for these questions, but oh well, I tried to avoid it and GrandFather threw me out in the open, so I guess I might as well ask now. ;-)

Replies are listed 'Best First'.
Re^4: Matching and combining two text files
by GrandFather (Saint) on Jan 23, 2012 at 23:43 UTC

    Asking about things you don't understand is smart and good. Not asking is the dumbest thing you can do because it wastes everyone's time and you learn nothing and keep repeating the same mistakes.

    I've always been taught that nice clean code, always has all variables declared before hand

    I'd get a new teacher if I were you! Code with all the declarations lumped together may look pretty to some eyes, but you gain no advantage whatsoever from declaring variables like that. Always declare variables in the smallest sensible scope and initialise them at the same time. That helps avoid a whole slew of bugs including reusing a variable name and ending up with subtle heisenbugs as a result.

    First, to answer a couple of questions you didn't ask. Use the three parameter form of open (open handle, mode, target) because providing an explicit mode ('>' or '<' for example) is both clearer and safer. Use lexical file handles because they are clearer and safer - safer because their scope is limited to the current block and using strict you are more likely to catch typos.

    The point about avoiding nesting is that the deeper the nesting goes the harder it is to figure out what the code does. If you have a simple test and can bail (as in the next if line) you don't have to worry about that case any more, it's all done and dusted.

    Slurping is where you read everything into an array. If that is followed by looping over the array using a for loop then very likely you can remove the array and use a while loop instead. That has two advantages: 1/ you can see from the code that you loop while there is stuff in the file (which gets sorta obscured by the array), and 2/ you only read one line of the file into memory at a time. Most often the second point isn't all that important, but if the file is huge it can be a killer. Neither reason is absolutely compelling, but slurping seldom has an advantage over iteration using a while loop so you might as well go with clearer and use the wile loop (iteration) form.

    True laziness is hard work