Page 1 of 1 [ 15 posts ] 

Mophesh
Hummingbird
Hummingbird

User avatar

Joined: 17 Jun 2008
Age: 33
Gender: Male
Posts: 21

08 Aug 2008, 4:58 pm

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?



Dokken
Veteran
Veteran

User avatar

Joined: 11 Oct 2007
Age: 45
Gender: Male
Posts: 998
Location: DeeSee/Merryland Area

08 Aug 2008, 5:24 pm

sounds like a personal problem. in other words, I cannot not help.



viska
Veteran
Veteran

User avatar

Joined: 26 Jan 2008
Age: 43
Gender: Male
Posts: 720
Location: Everytime you close your eyes: Lies, lies.

08 Aug 2008, 7:09 pm

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.



Mophesh
Hummingbird
Hummingbird

User avatar

Joined: 17 Jun 2008
Age: 33
Gender: Male
Posts: 21

08 Aug 2008, 7:38 pm

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).



viska
Veteran
Veteran

User avatar

Joined: 26 Jan 2008
Age: 43
Gender: Male
Posts: 720
Location: Everytime you close your eyes: Lies, lies.

08 Aug 2008, 9:01 pm

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.



Aaron_Mason
Veteran
Veteran

User avatar

Joined: 3 Jul 2005
Age: 39
Gender: Male
Posts: 511
Location: Bathurst, Australia

08 Aug 2008, 9:26 pm

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


Aaron_Mason
Veteran
Veteran

User avatar

Joined: 3 Jul 2005
Age: 39
Gender: Male
Posts: 511
Location: Bathurst, Australia

08 Aug 2008, 9:33 pm

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 08 Aug 2008, 9:38 pm, edited 1 time in total.

viska
Veteran
Veteran

User avatar

Joined: 26 Jan 2008
Age: 43
Gender: Male
Posts: 720
Location: Everytime you close your eyes: Lies, lies.

08 Aug 2008, 9:34 pm

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.



Mophesh
Hummingbird
Hummingbird

User avatar

Joined: 17 Jun 2008
Age: 33
Gender: Male
Posts: 21

08 Aug 2008, 9:46 pm

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?



Aaron_Mason
Veteran
Veteran

User avatar

Joined: 3 Jul 2005
Age: 39
Gender: Male
Posts: 511
Location: Bathurst, Australia

08 Aug 2008, 9:48 pm

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


viska
Veteran
Veteran

User avatar

Joined: 26 Jan 2008
Age: 43
Gender: Male
Posts: 720
Location: Everytime you close your eyes: Lies, lies.

08 Aug 2008, 11:28 pm

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);



gamefreak
Veteran
Veteran

User avatar

Joined: 30 Dec 2006
Age: 35
Gender: Male
Posts: 1,119
Location: Citrus County, Florida

08 Aug 2008, 11:51 pm

Never heard of Perl



Mophesh
Hummingbird
Hummingbird

User avatar

Joined: 17 Jun 2008
Age: 33
Gender: Male
Posts: 21

09 Aug 2008, 12:11 am

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?



Aaron_Mason
Veteran
Veteran

User avatar

Joined: 3 Jul 2005
Age: 39
Gender: Male
Posts: 511
Location: Bathurst, Australia

09 Aug 2008, 6:23 am

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


Mophesh
Hummingbird
Hummingbird

User avatar

Joined: 17 Jun 2008
Age: 33
Gender: Male
Posts: 21

09 Aug 2008, 11:16 am

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.