Support Wrong Planet Awareness!
| View previous topic :: View next topic |
| Author |
Message |
Mophesh Tufted Titmouse


Joined: Jun 18, 2008 Age: 16 Posts: 26
|
Posted: Fri Aug 08, 2008 5:58 pm Post subject: Ugh. Anyone good with Perl? |
|
|
OK, I'm kind of new to Perl (I just learned it a few days ago), and I'm having a ton of trouble with objects. I'm trying to port an IRC bot I made in Python to Perl, since Python can't handle math too well.
Python code:
| Code: |
class Charclass:
def __init__(self,name,clb,mw,mb,mc,lock,elem):
self.name = name
self.stru = clb[0]
self.stau = clb[1]
self.agiu = clb[2]
self.magu = clb[3]
# Max white/black magic levels
self.mw = mw
self.mb = mb
def showdesc(self,nicknm):
sdat("NOTICE "+nicknm+" :"+self.name+"\r\n")
sdat("NOTICE "+nicknm+" :Level bonus : Str: "+str(self.stru)+", Sta: "+str(self.stau)+", Agi: "+str(self.agiu)+", Mag: "+str(self.magu)+" \r\n")
sdat("NOTICE "+nicknm+" :White level : "+str(self.mw)+"\r\n")
sdat("NOTICE "+nicknm+" :Black level : "+str(self.mb)+"\r\n")
classes["war"] = Charclass("Warrior",[1,1,0,0],0,0,0,0)
|
Perl code:
| Code: |
{ package Charclass;
sub new {
my $self = {};
$self->{name} = shift; # line 32
$self->{stru} = shift;
$self->{stau} = shift;
$self->{agiu} = shift;
$self->{magu} = shift;
$self->{mw} = shift;
$self->{mb} = shift;
bless($self);
return $self;
}
sub showdesc {
my $self = shift;
my $fromnick = shift;
print $sock "NOTICE $fromnick :$self->{name}\r\n";
print $sock "NOTICE $fromnick :Stat adjust : Str: $self->{stru}, Sta: $self->{stau}, Agi: $self->{agiu}, Mag: $self->{magu} \r\n";
print $sock "NOTICE $fromnick :White level : $self->{mw}\r\n";
print $sock "NOTICE $fromnick :Black level : $self->{mb}\r\n";
}
}
$classes{"war"} = Charclass->new("Warrior",20,10,0,-10,0,0);
|
Right now, I have the Perl bot set up to where I can remotely call showdesc from the IRC channel. However, I have no idea what I'm doing here, and every time I try to get it to call showdesc, the following happens:
"Can't call method "showdesc" on an undefined value at rpgbot.pl line 133, <GEN0> line 32."
How do I fix this? Moreover, how do I use objects correctly? |
|
| Back to top |
|
Dokken Deinonychus


Joined: Oct 12, 2007 Age: 28 Posts: 334 Location: Bmore careful in da streets, Merryland
|
Posted: Fri Aug 08, 2008 6:24 pm Post subject: |
|
|
| sounds like a personal problem. in other words, I cannot not help. |
|
| Back to top |
|
viska Phoenix


Joined: Jan 27, 2008 Age: 27 Posts: 753 Location: Everytime you close your eyes: Lies, lies.
|
Posted: Fri Aug 08, 2008 8:09 pm Post subject: |
|
|
You don't have enough information here for us to help you. The package definition looks fine. You need to show us how you're trying to call the method.
You're doing $blah->showdesc(); and $blah is undefined.
$classes{"war"} = Charclass->new("Warrior",20,10,0,-10,0,0); #This line should be in your main package instead of your Charclass package. |
|
| Back to top |
|
Mophesh Tufted Titmouse


Joined: Jun 18, 2008 Age: 16 Posts: 26
|
Posted: Fri Aug 08, 2008 8:38 pm Post subject: |
|
|
| viska wrote: | You don't have enough information here for us to help you. The package definition looks fine. You need to show us how you're trying to call the method.
You're doing $blah->showdesc(); and $blah is undefined.
$classes{"war"} = Charclass->new("Warrior",20,10,0,-10,0,0); #This line should be in your main package instead of your Charclass package. |
OK, I've pastebinned the entire code here: http://pastebin.com/m736409ab
If what I'm doing is right, then I am calling the function from outside the Charclass package (each package is in its own set of braces). |
|
| Back to top |
|
viska Phoenix


Joined: Jan 27, 2008 Age: 27 Posts: 753 Location: Everytime you close your eyes: Lies, lies.
|
Posted: Fri Aug 08, 2008 10:01 pm Post subject: |
|
|
Your syntax for accessing members of arrays is wrong. If you have:
my @array = ('this', 'is', 'a', 'test', 'blah');
And you want to access 'test', you need
$array[3]
NOT
@array[3] #this is wrong
It's confusing, but it's perl. Larry wall says it's like english, when talking about a group of apples you would say "These apples", but when talking about a specific one, you would say "this" apple. These = @ and this = $.
I highly recommend using "use strict;" and "use warnings;" in your script. You will be required to declare your variables ahead of time (just use my... my @array; my %hash; my $scalar; .. they take lexical scope), but your error messages will be much more useful. |
|
| Back to top |
|
Aaron_Mason Phoenix


Joined: Jul 04, 2005 Age: 23 Posts: 630 Location: Bathurst, Australia
|
Posted: Fri Aug 08, 2008 10:26 pm Post subject: |
|
|
I found the problem.
@line[4] must have had a trailling carriage return that chop() left behind. The script would try to get $classes{'war\r'} when you called the .dc command, try to access the class, and die horribly. Try a chomp() after the chop, that should fix it.
A new problem has come up, though, an undefined value being used as a reference. It occurs with the first value you attempt to print out. _________________ We are one, we are strong... the more you hold us down, the more we press on - Creed, "What If"
AS is definitive. Reality is frequently inaccurate.
I'm the same as I was when I was six years old - Modest Mouse |
|
| Back to top |
|
Aaron_Mason Phoenix


Joined: Jul 04, 2005 Age: 23 Posts: 630 Location: Bathurst, Australia
|
Posted: Fri Aug 08, 2008 10:33 pm Post subject: |
|
|
Fixed it,
You will need to send the socket reference to each of your functions that requires it. i.e. $templol->showdesc($sock, $fromnick);
then add my $sock = shift; after my $self etc etc
Works great now
EDIT: Pasted. http://pastebin.com/m53a8006b _________________ We are one, we are strong... the more you hold us down, the more we press on - Creed, "What If"
AS is definitive. Reality is frequently inaccurate.
I'm the same as I was when I was six years old - Modest Mouse
Last edited by Aaron_Mason on Fri Aug 08, 2008 10:38 pm; edited 1 time in total |
|
| Back to top |
|
viska Phoenix


Joined: Jan 27, 2008 Age: 27 Posts: 753 Location: Everytime you close your eyes: Lies, lies.
|
Posted: Fri Aug 08, 2008 10:34 pm Post subject: |
|
|
The problem I mentioned above happens at least 10 times in the program and will stop anything from working.
Edit, just checked.. it works w/o strict and warnings. |
|
| Back to top |
|
Mophesh Tufted Titmouse


Joined: Jun 18, 2008 Age: 16 Posts: 26
|
Posted: Fri Aug 08, 2008 10:46 pm Post subject: |
|
|
| viska wrote: | Your syntax for accessing members of arrays is wrong. If you have:
my @array = ('this', 'is', 'a', 'test', 'blah');
And you want to access 'test', you need
$array[3]
NOT
@array[3] #this is wrong
It's confusing, but it's perl. Larry wall says it's like english, when talking about a group of apples you would say "These apples", but when talking about a specific one, you would say "this" apple. These = @ and this = $.
I highly recommend using "use strict;" and "use warnings;" in your script. You will be required to declare your variables ahead of time (just use my... my @array; my %hash; my $scalar; .. they take lexical scope), but your error messages will be much more useful. |
OK, I tried all that. warnings is really helpful. But now I've got another problem: creating Charclass instances. Here's the error I kept getting until I just decided to comment it (and the code used to call showdesc) out:
syntax error at rpgbot.pl line 66, near "$classes{"
Execution of rpgbot.pl aborted due to compilation errors.
Any help? |
|
| Back to top |
|
Aaron_Mason Phoenix


Joined: Jul 04, 2005 Age: 23 Posts: 630 Location: Bathurst, Australia
|
Posted: Fri Aug 08, 2008 10:48 pm Post subject: |
|
|
It helps if we know what line 66 is. What I have shows line 66 as an empty line. _________________ We are one, we are strong... the more you hold us down, the more we press on - Creed, "What If"
AS is definitive. Reality is frequently inaccurate.
I'm the same as I was when I was six years old - Modest Mouse |
|
| Back to top |
|
viska Phoenix


Joined: Jan 27, 2008 Age: 27 Posts: 753 Location: Everytime you close your eyes: Lies, lies.
|
Posted: Sat Aug 09, 2008 12:28 am Post subject: |
|
|
Guessing here. If your line says:
my $classes{"war"} = Charclass->new("Warrior",20,10,0,-10,0,0);
You need to change it to:
my %classes;
$classes{"war"} = Charclass->new("Warrior",20,10,0,-10,0,0); |
|
| Back to top |
|
gamefreak I don't have a monopoly, I'm just competitive

Joined: Dec 31, 2006 Age: 18 Posts: 1176 Location: Spring Hill, Florida
|
Posted: Sat Aug 09, 2008 12:51 am Post subject: |
|
|
| Never heard of Perl |
|
| Back to top |
|
Mophesh Tufted Titmouse


Joined: Jun 18, 2008 Age: 16 Posts: 26
|
Posted: Sat Aug 09, 2008 1:11 am Post subject: |
|
|
| viska wrote: | Guessing here. If your line says:
my $classes{"war"} = Charclass->new("Warrior",20,10,0,-10,0,0);
You need to change it to:
my %classes;
$classes{"war"} = Charclass->new("Warrior",20,10,0,-10,0,0); |
Although I tried that earlier with no success, I decided to try it again, though this time with success.
I'm still having trouble with showdesc, though. Is it the way I'm referring to the object's data, or what? |
|
| Back to top |
|
Aaron_Mason Phoenix


Joined: Jul 04, 2005 Age: 23 Posts: 630 Location: Bathurst, Australia
|
Posted: Sat Aug 09, 2008 7:23 am Post subject: |
|
|
| Mophesh wrote: | | I'm still having trouble with showdesc, though. Is it the way I'm referring to the object's data, or what? |
Did you even read my response?
| Aaron_Mason wrote: | Fixed it,
You will need to send the socket reference to each of your functions that requires it. i.e. $templol->showdesc($sock, $fromnick);
then add my $sock = shift; after my $self etc etc
Works great now
EDIT: Pasted. http://pastebin.com/m53a8006b |
_________________ We are one, we are strong... the more you hold us down, the more we press on - Creed, "What If"
AS is definitive. Reality is frequently inaccurate.
I'm the same as I was when I was six years old - Modest Mouse |
|
| Back to top |
|
Mophesh Tufted Titmouse


Joined: Jun 18, 2008 Age: 16 Posts: 26
|
Posted: Sat Aug 09, 2008 12:16 pm Post subject: |
|
|
| Aaron_Mason wrote: | | Mophesh wrote: | | I'm still having trouble with showdesc, though. Is it the way I'm referring to the object's data, or what? |
Did you even read my response?
| Aaron_Mason wrote: | Fixed it,
You will need to send the socket reference to each of your functions that requires it. i.e. $templol->showdesc($sock, $fromnick);
then add my $sock = shift; after my $self etc etc
Works great now
EDIT: Pasted. http://pastebin.com/m53a8006b |
|
Sorry, didn't see it... Anyway, thanks a lot, I got it working. |
|
| Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|