Discussion | Articles | Blogs | Books | Contact Us | Chat | Shop | Search
  WrongPlanet.net
User Stats
   Members: 22,680
   Online Now: 342



People Online:
Visitors: 246
Members: 96
New Today: 0
New Yesterday: 21
Latest: mortsttam

Search
Google
Web WP.net



  Aspie Affection
Support Wrong Planet Awareness!
Ugh. Anyone good with Perl?

 
Post new topic   Reply to topic    Wrong Planet Forums Forum Index -> Computers, Math, Science, and Technology
View previous topic :: View next topic  
Author Message
Mophesh
Tufted Titmouse
Tufted Titmouse


Joined: Jun 18, 2008
Age: 16
Posts: 26

PostPosted: Fri Aug 08, 2008 5:58 pm    Post subject: Ugh. Anyone good with Perl? Reply with quote

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
View user's profile Send private message
Dokken
Deinonychus
Deinonychus


Joined: Oct 12, 2007
Age: 28
Posts: 334
Location: Bmore careful in da streets, Merryland

PostPosted: Fri Aug 08, 2008 6:24 pm    Post subject: Reply with quote

sounds like a personal problem. in other words, I cannot not help.
Back to top
View user's profile Send private message Visit poster's website AIM Address
viska
Phoenix
Phoenix


Joined: Jan 27, 2008
Age: 27
Posts: 753
Location: Everytime you close your eyes: Lies, lies.

PostPosted: Fri Aug 08, 2008 8:09 pm    Post subject: Reply with quote

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
View user's profile Send private message
Mophesh
Tufted Titmouse
Tufted Titmouse


Joined: Jun 18, 2008
Age: 16
Posts: 26

PostPosted: Fri Aug 08, 2008 8:38 pm    Post subject: Reply with quote

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
View user's profile Send private message
viska
Phoenix
Phoenix


Joined: Jan 27, 2008
Age: 27
Posts: 753
Location: Everytime you close your eyes: Lies, lies.

PostPosted: Fri Aug 08, 2008 10:01 pm    Post subject: Reply with quote

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
View user's profile Send private message
Aaron_Mason
Phoenix
Phoenix


Joined: Jul 04, 2005
Age: 23
Posts: 630
Location: Bathurst, Australia

PostPosted: Fri Aug 08, 2008 10:26 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
Aaron_Mason
Phoenix
Phoenix


Joined: Jul 04, 2005
Age: 23
Posts: 630
Location: Bathurst, Australia

PostPosted: Fri Aug 08, 2008 10:33 pm    Post subject: Reply with quote

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 Smile

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
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
viska
Phoenix
Phoenix


Joined: Jan 27, 2008
Age: 27
Posts: 753
Location: Everytime you close your eyes: Lies, lies.

PostPosted: Fri Aug 08, 2008 10:34 pm    Post subject: Reply with quote

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
View user's profile Send private message
Mophesh
Tufted Titmouse
Tufted Titmouse


Joined: Jun 18, 2008
Age: 16
Posts: 26

PostPosted: Fri Aug 08, 2008 10:46 pm    Post subject: Reply with quote

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
View user's profile Send private message
Aaron_Mason
Phoenix
Phoenix


Joined: Jul 04, 2005
Age: 23
Posts: 630
Location: Bathurst, Australia

PostPosted: Fri Aug 08, 2008 10:48 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
viska
Phoenix
Phoenix


Joined: Jan 27, 2008
Age: 27
Posts: 753
Location: Everytime you close your eyes: Lies, lies.

PostPosted: Sat Aug 09, 2008 12:28 am    Post subject: Reply with quote

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
View user's profile Send private message
gamefreak
I don't have a monopoly, I'm just competitive


Joined: Dec 31, 2006
Age: 18
Posts: 1176
Location: Spring Hill, Florida

PostPosted: Sat Aug 09, 2008 12:51 am    Post subject: Reply with quote

Never heard of Perl
Back to top
View user's profile Send private message Send e-mail
Mophesh
Tufted Titmouse
Tufted Titmouse


Joined: Jun 18, 2008
Age: 16
Posts: 26

PostPosted: Sat Aug 09, 2008 1:11 am    Post subject: Reply with quote

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
View user's profile Send private message
Aaron_Mason
Phoenix
Phoenix


Joined: Jul 04, 2005
Age: 23
Posts: 630
Location: Bathurst, Australia

PostPosted: Sat Aug 09, 2008 7:23 am    Post subject: Reply with quote

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 Smile

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
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
Mophesh
Tufted Titmouse
Tufted Titmouse


Joined: Jun 18, 2008
Age: 16
Posts: 26

PostPosted: Sat Aug 09, 2008 12:16 pm    Post subject: Reply with quote

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 Smile

EDIT: Pasted. http://pastebin.com/m53a8006b


Sorry, didn't see it... Anyway, thanks a lot, I got it working.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Wrong Planet Forums Forum Index -> Computers, Math, Science, and Technology All times are GMT - 5 Hours
Page 1 of 1

 
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

Wrong PlanetTM Copyright 2004-2008, Alex Plank and Yellow Sneaker Media, LLC
Alex Plank  Aspie Affection 

Terms of Service - You must read this as a user of Wrong Planet

RSS Feed Add to Google Add to My Yahoo!

Subscribe: Wrong Planet News  Wrong Planet Forums

Privacy Policy

Asperger's is not a disease

fine art