Joined: 11 Mar 2004 Posts: 314 Location: El Paso, Texas
Posted: Tue Nov 09, 2004 2:12 am Post subject:
Hey, I'm currently making a module called SBAI (Smart Bot Artificial Intelligence) to enhance the way you ask bots to do commands. Example: Instead of "!menu", you'd say "Show me the menu". Yes, seems very pointless, but heck, you can do more with it if you use your immagination. It's also for fun.
Anyways, my code kinda works.. It just doesnt relay the response back to bot.pl so it can send it to the user.
Here's my code.
**EDIT: Took it out because everyone was making fun of me. Now nobody gets the cookie.**
Not quite sure what's wrong with it. First person to help gets a cookie.
You should always use strict; and use warnings;, especially when you're trying to create a new module.
You're not using my on several variables, which will only cause you lots of problems down the line.
You're using a variable $sbai in many places, but where is it coming from? I suspect, you think $sbai is a reference to your object, but this is the class code, where are you actually creating the object?
None of the methodes in your SBAI package ever get a reference to the SBAI object. The first line of any object oriented method (function) should be my $this = shift;. Sometimes, people use $self.
You use my in front of variable names when you declare them, not when you use them. My won't mess up your hashes - you get the errors and warnings for a reason.
What did you edit in your code? Did you add my $sbai = shift; to your new method? That's not where you want it, you want it in all of the other methods.
And why are you creating an object that has an array instead of a hash?
Code:
sub new {<br /> my $class = shift;<br /> my $sbai = shift;<br /> # notice it's an array, not a hash<br /> return bless [], $class;<br />}
Why an array and not a hash?
Where did you find this code? Or did you really make it from scratch?
Joined: 03 May 2006 Posts: 2292 Location: Colorado
Posted: Tue Nov 09, 2004 11:00 pm Post subject:
This is a pretty good example of code that hasn't been tested even a little bit.
When building modules, I myself, like to do them in little bits. I take the smallest part and grow the module form there. Generaly that means makeing two files, the module file, and a test file. The test file is just the minimal amount needed to use that module. They both start with just a few lines, then as functions are added to the module, i add code to test those functions in the test script. In the end you have a working module, that you know every peice of is constantly tested, and all along the route you have working code.
Your code on the other hand looks like you had an idea, and tried to program the whole thing all at once. Doing it in little bits and constantly testing will allow you to catch errors like "if ($msg = @k1) {" much earlier. That bit of code sets $msg equal to the length of the array k1. Which i'm guessing is not what you want it to do. _________________ Eric256
Proud previous owner and current admin of Bot-depot.com