User Control Panel
Advertisements

HELP US, HELP YOU!

Hashes in modules

 
Post new topic   Reply to topic    Bot Depot Forum Index -> Perl
View unanswered posts
Author Message
Nate
God Like
God Like


Joined: 12 Nov 2003
Posts: 553

Reputation: 41.3Reputation: 41.3Reputation: 41.3Reputation: 41.3

PostPosted: Sun Dec 14, 2003 10:59 pm    Post subject: Reply with quote

Hey, I was trying to write a module called CKS::Chaos which is basicly a bot brain inside a module, which would make the 'technical' stuff like editing how it handles warners or gutting apart its reply system easier than it has been for the more recent (and muy complex) ChaosBot Programs.

Anyway, I tried copying code from the new() sub in the module. And this is what I got:

Code:
my $chaos = new CKS::Chaos (<br />     Name => "Ultimate Chaos",<br />     Folder => "./info",<br />     Debug => 1);


And in the module I did the thing like:
my $hash = rep($that) || $that

And this is what ends up getting sent to the module:

"CKS::Chaos", "Name", "Ultimate Chaos", "Folder", "./info", "Debug", "1"

The "CKS::Chaos" part is the part referred to as $self usually.. but how do you get it to only get the values from the hash and not every single word?
Back to top
Mojave
Almost An Agent
Almost An Agent


Joined: 01 Nov 2003
Posts: 1434

Reputation: 66.4

PostPosted: Sun Dec 14, 2003 11:26 pm    Post subject: Reply with quote

I wouldn't advise using the function name new unless your module is a class, since new is 99.99% of the time used to instantiate an object by calling its constuctor.

Now, on to hashes and how they relate to lists. A hash is really just a special kind of list where every two items in the list are key/value pairs in the hash. So you can take a list and convert it into a hash very easily:

Code:
@list = ( "Name", "Ultimate Chaos", "Folder", "./info", "Debug", "1" );<br />%hash = @list;


That will convert (or in some languages, you might say cast) a list into a hash.

One important note that might make things more understandable: "=>" is just a synonym for ",".

Now, if you have a function called new and you call it the way you did, then to get the class name (module name) and also get the arguments into a hash, you would do:

Code:
sub new<br />{<br />  my $class_name = shift;<br />  my %hash = @_;<br />}


That shifts off the first item from the argument list into the variable $class_name and then takes the remaining items in the list, @_ and casts them into the hash %hash.
Back to top
Nate
God Like
God Like


Joined: 12 Nov 2003
Posts: 553

Reputation: 41.3Reputation: 41.3Reputation: 41.3Reputation: 41.3

PostPosted: Tue Dec 16, 2003 8:04 pm    Post subject: Reply with quote

Ok, I decided to name the module AI::CKS::Chaos... and when I try to use it (I just did a simple test of displaying the hashes) it gives me an error:

Code:
Loaded AI::CKS::Chaos<br /><br />Undefined subroutine &AI::CKS::Chaos called at chaos.pl line 13.<br />Press any key to continue . . .


Here's the module:
Code:
#############################################<br />## Chaos A.I. Technology                   ##<br />##-----------------------------------------##<br />## AI::CKS::Chaos - The CKS Ultimate Chaos ##<br />##     Artificial Intelligence module.     ##<br />##-----------------------------------------##<br />$AUTHOR = "Kirsle";  ## This module is     ##<br />$VERSION = "1.00";   ## under the GPL      ##<br />$COPYRIGHT = "2003"; ## license.           ##<br />#############################################<br /><br />use LWP::Simple;<br /><br /># Declare the default hash information.<br />%fields = (<br />   Name     => "Ultimate Chaos",<br />   Folder   => "./",<br />   Debug    => 0,<br />   Init     => "CONNECT",<br />);<br /><br /># Sub: new ($name,$folder,$debug,$etc);<br />#<br /># Creates a new AI::CKS::Chaos instance. Variables<br /># must be sent as a hash:<br /># $cks = new AI::CKS::Chaos (<br />#   Name   => "Ultimate Chaos",<br />#   Folder => "./bot_files",<br />#   Debug  => 1,<br /># );<br />sub new {<br />   my $class = shift;<br />   my %hash = @_;<br /><br />   foreach my $item (keys %hash) {<br />  print $item . " = " . $hash{$item} . "\n";<br />   }<br />}<br /><br />print "Loaded AI::CKS::Chaos\n\n";<br /><br />1;


And here's the Perl test file:
Code:
#!/usr/bin/perl<br /><br /># Chaos AI Technology<br /># AI::CKS::Chaos Module Example Script<br /><br /># Use the local library.<br />use lib "./lib";<br /><br /># Require the Chaos module.<br />use AI::CKS::Chaos;<br /><br /># Create a new Chaos bot.<br />my $chaos = new AI::CKS::Chaos (<br />   Name   => "Zoe Aidenn",<br />   Folder => "./zoe",<br />   Debug  => 1,<br />);<br /><br /># This is just a test.<br />sleep(100);


Also, does anyone have any resources on how POD's work?
Back to top
Nate
God Like
God Like


Joined: 12 Nov 2003
Posts: 553

Reputation: 41.3Reputation: 41.3Reputation: 41.3Reputation: 41.3

PostPosted: Tue Dec 16, 2003 8:07 pm    Post subject: Reply with quote

Ohh, wait, I discovered why it wasn't using it. I forgot to put the "package AI::CKS::Chaos" at the top. Razz

Anyway, how to pod's work?
Back to top
eric256
The Keymaker
The Keymaker


Joined: 03 May 2006
Posts: 2292
Location: Colorado
Reputation: 47Reputation: 47Reputation: 47Reputation: 47Reputation: 47

PostPosted: Tue Dec 16, 2003 8:09 pm    Post subject: Reply with quote

Pod Manual
_________________
Eric256
Proud previous owner and current admin of Bot-depot.com
Back to top
Nate
God Like
God Like


Joined: 12 Nov 2003
Posts: 553

Reputation: 41.3Reputation: 41.3Reputation: 41.3Reputation: 41.3

PostPosted: Tue Dec 16, 2003 8:24 pm    Post subject: Reply with quote

What is the point of pod's anyway? Is there some special kind of parser that actually cares about them?

And are they a requirement?
Back to top
eric256
The Keymaker
The Keymaker


Joined: 03 May 2006
Posts: 2292
Location: Colorado
Reputation: 47Reputation: 47Reputation: 47Reputation: 47Reputation: 47

PostPosted: Wed Dec 17, 2003 5:09 am    Post subject: Reply with quote

not a requirment.

yes there are things to convert pod to other formats.... HTML among others.

_________________
Eric256
Proud previous owner and current admin of Bot-depot.com
Back to top
Nate
God Like
God Like


Joined: 12 Nov 2003
Posts: 553

Reputation: 41.3Reputation: 41.3Reputation: 41.3Reputation: 41.3

PostPosted: Fri Dec 19, 2003 5:04 pm    Post subject: Reply with quote

Ok, is this the correct pod format:

Code:
=pod<br /><br />=item NEW<br /><br />Create a new instance of the module.<br /><br />=cut


Or is it supposed to be like this:

Code:
=head1 NEW<br /><br />Create a new instance of the module.<br /><br />=cut


?
Back to top
eric256
The Keymaker
The Keymaker


Joined: 03 May 2006
Posts: 2292
Location: Colorado
Reputation: 47Reputation: 47Reputation: 47Reputation: 47Reputation: 47

PostPosted: Fri Dec 19, 2003 7:17 pm    Post subject: Reply with quote

Please try them your self. If you find it gives you errors then post something along the lines of....i tried X and it gave me error *lazy*. Any idea how to fix it?


BTW: regarding your first post, it should be ref not rep.

_________________
Eric256
Proud previous owner and current admin of Bot-depot.com
Back to top
Nate
God Like
God Like


Joined: 12 Nov 2003
Posts: 553

Reputation: 41.3Reputation: 41.3Reputation: 41.3Reputation: 41.3

PostPosted: Sat Dec 20, 2003 2:20 am    Post subject: Reply with quote

Ah. Well either of those pod examples from my last post here work just fine... I'm just wondering, if some parser attempted to read the pod's, would it be looking for seperate =pod and =cut, or would =head1 and =cut work just the same?
Back to top
Mojave
Almost An Agent
Almost An Agent


Joined: 01 Nov 2003
Posts: 1434

Reputation: 66.4

PostPosted: Sat Dec 20, 2003 3:49 am    Post subject: Reply with quote

Eric sent you documentation. Wouldn't it be easier to just look up what you want in there rather than ask here? Or you could just trial and error it.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Bot Depot Forum Index -> Perl All times are GMT
Page 1 of 1

 



Protected by phpBB Security phpBB-TweakS
phpBB Security Has Blocked 9 Exploit Attempts.
Antispam Captcha Mod by phpbb-security.com
Powered by phpBB © 2001, 2005 phpBB Group