in reply to Subnet Overlap

Hello, I've reviewed your code and I have a number of comments to make. First things first: I hate to break it to you, but a lot of the code could be replaced by a couple of lines of code from a CPAN module, like Net::CIDR or NetAddr::Ip. I'd also wager a beer that there's a bug or two present in your code. That is not to mean you're not a good programmer, but IP address arithmetic is notoriously hard to get right. By using either of these modules, you're getting code that has been thoroughly exercised by people in many contexts, over many years.

Apart from the design design to clear the screen when the program is run (something that many people find irritating), the execution of the program 'clear' only happens to work because you're not using strict. Thus, the perl interpreter first tries to see if clear is an internal routine, and after having given up, it ends up interpreting the bare word as a string. So it works, but it's a dodgy practice.

One of the reasons I think that the code may have bugs is because you are using string comparisons ($match gt 1) rather than numeric comparisons ($match > 1). Comparing against one may be safe, but remember that 9 > 10 evaluates to true. Say what you mean, to be certain.

Elsewhere in the code, a comment says:

Due to a bug in Perl, after the split, the new variables need to have an mathmatical operation performed in order to make them numeric variables rather than string variables.

This is quite untrue and more indicative of your own misunderstanding of how Perl deals with strings. If you put a variable in an arithmetic expression, Perl will treat it as a number. Thus "2" + "2" equals 4, but "2" . "2" equals "22". split will return just what you want. Numbers or strings, it's your call.

Briefly in passing, consider using the 3-arg form of open, don't call functions with ampersand (&func;) and do use strict.

Also, rather than dealing with the mechanics of opening and closing files, you can ditch a lot of code by just reading from STDIN and writing to STDOUT, and deal with what file is to be read, and to what file the results should be written, from the shell. Less code, less things to go wrong.

I must apologise if this all comes out sounding a bit harsh, that is not my intent, but I don't have the time to be more diplomatic. We are all here to learn, and if you learn from this post, then we're one step ahead.

Keep perling!

• another intruder with the mooring in the heart of the Perl

Replies are listed 'Best First'.
Re^2: Subnet Overlap
by ikegami (Patriarch) on Aug 21, 2007 at 20:22 UTC

    This is quite untrue and more indicative of your own misunderstanding of how Perl deals with strings

    Actually, the OP was correct. split does return strings, and he does need to force them into numbers before using & on them.

    my $i = my $x = (split(/\./, '205.0.0.0'))[0]; my $j = my $y = (split(/\./, '123.0.0.0'))[0]; my $ij = $i & $j; $x += 0; $y += 0; my $xy = $x & $y; print("$i & $j = $ij\n"); # 205 & 123 = 001 print("$x & $y = $xy\n"); # 205 & 123 = 73

    Your example shows that +, expecting a number, will convert the string to a number, but the OP is using & which is acts differently for strings and numbers.

    The OP was wrong in calling it a bug, though.

      Thanks for clarifying that for me. Yeah, I should have used a word other than "bug" as it's not truly a bug. Thanks for your feedback. -B
Re^2: Subnet Overlap
by bfarley (Initiate) on Aug 22, 2007 at 03:01 UTC
    Thanks for your input. I haven't been using Perl for very long, so I am sure that there are several other ways to do what I did. My first objective was just to see if I could solve a problem. Although using a CPAN module may have been easier and already tested, it doesn't really give me the opportunity to solve the problem on my own. I never really thought too much about the clear screen lines, but I will keep that in mind for any future code I post. I'll be sure to start using strict. As you mentioned, it's just good practice. I am correct in my statement about what is required after using split, however, I should have used better terminology in my comment, and not described it as a bug. I'm not familiar with the 3-arg form of open, so that is something I will look into. Good point on the STDIN and STDOUT. I will keep this in mind for any future code. Thanks again for the feedback, It will definitely give me some things to research and think about for any additional code I write. -B