News Info Software Resources Communities Research Contact
SNS Journal Documentation
Resources >SNS Journal >Development >Creating Chat Tags (Part 1) >

Creating Chat Tags (Part 1)

December 20, 2001

James Estes
SNS Software Developer
housejester@hotmail.com

In Tags Are Fun! in the SNS User Journal, Perlchat's tagging feature was explained. Now that you know how to use tags, you need to know how to create new ones. When a user types a tag, the chat room finds the handler for that tag and runs it. Creating a new tag is as simple as creating the handler for it.

What is a tag handler?

Tag handlers are Perl Objects that extend the SHADOW_perlchat::ChatTag class. When the tag is sent, the chat room will get an instance of the Handler's class, then call Run on it. The arguments that the user passed to the tag will be passed on to the Run method. (However, in OO Perl, the first argument to the method will always be a reference to the object itself).
So, for example, the hello tag handler described in Tags Are Fun! would look like:

hello.pm
------------
package SHADOW_perlchat::ChatTag::hello;

@ISA = qw( SHADOW_perlchat::ChatTag );
use strict;

sub Run{
	my $self = shift;
	my $to = shift;

	my $user_name = $self->GetUserName;

	if ( ! defined $to){
		$to = "everyone";
	}else{
		my $size = scalar( @_ );
		while ($size > 0){
			my $nm = shift;
			if($size > 1){
				$to.=", $nm";
			}else{
				$to.=", and $nm";
			}
			$size = scalar( @_ );
		}
	}

	return "$user_name says hello to $to";
}

1;
-------------

The module should be put in the SHADOW_perlchat/modules/taglib directory.

How do I know who typed the tag?

There are several methods that Handlers inherit from ChatTag, the one for the name of the user is GetUserName. So, to get the name of the user, just call $self->GetUserName;

More to come

That's all for Part 1. Next time I'll be talking about how to handle tags on the client side, how to write multi-lingual tags, and writing more complex tags (maybe a [kick userid] tag).



[top]

Content file last updated on 12/20/2001 at 15:29

Unique page hits = 431

Total page hits = 1517