root/abbreviation/trunk/lib/abbreviation.pm

Revision 130 (checked in by miyagawa, 19 years ago)

add All CAPS

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 package abbreviation;
2
3 use strict;
4 use vars qw($VERSION);
5 $VERSION = '0.02';
6
7 sub import {
8     my($class, @pkg) = @_;
9     return unless @pkg;
10
11     for my $pkg (@pkg) {
12         eval qq(require $pkg);
13         die if $@ && $@ !~ /^Can't locate .*? at \(eval /; #';
14         my $abbr = _abbr($pkg) or next;
15         no strict 'refs';
16         *{$abbr . '::'} = *{$pkg . '::'};
17     }
18 }
19
20 sub _abbr {
21     my $pkg = shift;
22
23     # Top level => nothing
24     return unless $pkg =~ /::/;
25
26     my @pkg = split /::/, $pkg;
27     my $lastone = pop @pkg;
28
29     # Mission:
30     # Foo::Bar::Baz -> F::B::Baz
31     # Foo::bar::Baz -> F::b::Baz
32     # FooBar::Bar::Baz -> FB::B::Baz
33     # FOO::Bar -> F::Bar
34     return join '::', (map {
35         s/^([A-Z])[A-Z0-9]+$/$1/; # FOO -> F
36         tr/A-Z0-9//cd;
37         $_;
38     } @pkg), $lastone;
39 }
40
41 1;
42
43 __END__
44 =head1 NAME
45
46 abbreviation - Perl pragma to abbreviate class names
47
48 =head1 SYNOPSIS
49
50   use abbreviation qw(Very::Long::ClassName::Here);
51  
52   my $obj = Very::Long::ClassName::Here->new;
53   my $obj = V::L::CN::Here->new;        # same
54
55 =head1 DESCRIPTION
56
57 Tired of typing long class name? use abbreviation for that.
58
59 =head1 TRICK AND CAVEAT
60
61 Dynamic package name aliasing can be implemented via:
62
63 =over 4
64
65 =item *
66
67 symbol table aliasing. (import.pm)
68
69 =item *
70
71 dynamic inheritance. (namespace.pm)
72
73 =back
74
75 Both has virtue and vice. Currently, abbreviation.pm takes the
76 B<former>. This may change in the future.
77
78 =head1 AUTHOR
79
80 Tatsuhiko Miyagawa <miyagawa@bulknews.net>
81
82 This library is free software; you can redistribute it and/or modify
83 it under the same terms as Perl itself.
84
85 =head1 SEE ALSO
86
87 L<import>, L<namespace>.
88
89 =cut
Note: See TracBrowser for help on using the browser.