10 our $VERSION = '0.05';
12 use constant { COLOR_TAG => "\00307" };
16 @attributes = qw/tag name version desc author email url banner/;
23 $class = ref($class) || $class;
26 my $caller = (caller 0)[0];
27 $opts{tag} ||= ($caller =~ /([^:]*):*$/) ? $1 : 'UNKNOWN';
28 $opts{name} ||= $opts{tag};
29 $opts{desc} ||= $opts{name};
30 $opts{version} ||= do { no strict 'refs'; ${$caller.'::VERSION'} }
33 my $email = $opts{email};
34 $email = "<$email>" if defined $email;
36 my $who = join ', ', grep defined, $opts{author}, $email, $opts{url};
37 $who = " ($who)" if length $who;
38 $opts{banner} = $opts{desc} . $who;
40 my $ph = bless \do { local *PH }, $class;
43 *$ph->{$_} = $opts{$_} for @attributes;
46 Xchat::register(@opts{qw/name version banner/}, sub {
48 if (*$ph->{atexit_id}) {
49 my @callbacks = sort { *$ph->{atexit}{$b} <=> *$ph->{atexit}{$a} }
50 keys %{*$ph->{atexit}};
51 $_->($ph) for @callbacks;
55 $ph->add_atexit($opts{unload}) if $opts{unload};
57 $ph->print("\002", $ph->name, ' v', $ph->version,
58 " loaded\002 (", $ph->banner, ")\n");
65 eval "sub $_ { my \$ph = \$_[0]; *\$ph->{$_} }; 1" or die $@ for @attributes;
74 my $data = join '', *$ph->{buf}, @_;
76 while ((my $i = index $data, "\n") >= 0) {
77 Xchat::print(COLOR_TAG . $ph->tag . "\t" . (substr $data, 0, ($i + 1), ''));
86 sub printf { shift->print(sprintf @_) }
91 if (length $ph->{buf}) {
99 my ($ph, $callback) = @_;
101 if (!*$ph->{atexit_id}) {
102 tie my %h, 'Tie::RefHash';
103 *$ph->{atexit} = \%h;
106 return *$ph->{atexit}{$callback} = ++*$ph->{atexit_id};
110 my ($ph, $callback) = @_;
112 if (exists *$ph->{atexit}{$callback}) {
113 delete *$ph->{atexit}{$callback};
122 (defined $ph && $ph->isa(__PACKAGE__)) ? $ph : shift->new(@_);
125 sub PRINT { shift->print(@_) }
127 sub PRINTF { shift->printf(@_) }