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");
64 eval "sub $_ { my \$ph = \$_[0]; *\$ph->{$_} }" for @attributes;
73 my $data = join '', *$ph->{buf}, @_;
75 while ((my $i = index $data, "\n") >= 0) {
76 Xchat::print(COLOR_TAG . $ph->tag . "\t" . (substr $data, 0, ($i + 1), ''));
85 sub printf { shift->print(sprintf @_) }
90 if (length $ph->{buf}) {
98 my ($ph, $callback) = @_;
100 if (!*$ph->{atexit_id}) {
101 tie my %h, 'Tie::RefHash';
102 *$ph->{atexit} = \%h;
105 return *$ph->{atexit}{$callback} = ++*$ph->{atexit_id};
109 my ($ph, $callback) = @_;
111 if (exists *$ph->{atexit}{$callback}) {
112 delete *$ph->{atexit}{$callback};
121 (defined $ph && $ph->isa(__PACKAGE__)) ? $ph : shift->new(@_);
124 sub PRINT { shift->print(@_) }
126 sub PRINTF { shift->printf(@_) }