root/capitalization/trunk/capitalization.pm

Revision 1560 (checked in by miyagawa, 14 years ago)

0.03

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 package capitalization;
2
3 use strict;
4 use vars qw($VERSION);
5 $VERSION = 0.03;
6
7 use Devel::Symdump;
8
9 my %done;
10
11 sub unimport {
12     my($class, @mods) = @_;
13     for my $mod (@mods) {
14         next if $done{$mod};
15
16         my $file = mod2file($mod);
17         require $file unless $INC{$file};
18
19         my $dump = Devel::Symdump->new($mod);
20         for my $meth (map { s/^\Q$mod\E:://; $_ } $dump->functions) {
21             my $new = nocap($meth);
22             if ($new ne $meth) {
23                 no strict 'refs';
24                 *{"$mod\::$new"} = \&{"$mod\::$meth"};
25             }
26         }
27         $done{$mod} = 1;
28     }
29 }
30
31 sub nocap {
32     my $method = shift;
33     $method =~ s/(?<=[a-z])([A-Z]+)/"_" . lc($1)/eg;
34     $method =~ tr/A-Z/a-z/;
35     return $method;
36 }
37
38 sub mod2file {
39     my $mod = shift;
40     $mod =~ s!::!/!g;
41     return "$mod.pm";
42 }
43
44 1;
45 __END__
46
47 =head1 NAME
48
49 capitalization - no capitalization on method names
50
51 =head1 SYNOPSIS
52
53   use XML::DOM;
54   no capitalization 'XML::DOM';
55
56   my $parser = XML::DOM::Parser->new;
57
58   # no capitalization ..
59   my $nodes = $parser->get_elements_by_tag_name("Foo");
60
61   # this can be OK
62   my $nodes = $parser->getElementsByTagName("Foo");
63
64
65 =head1 DESCRIPTION
66
67 capitalization.pm allows you to use familiar style on method naming.
68
69 =head1 RULES
70
71 =over 1
72
73 =item Lower case character followed by upper case sequence would be
74 splitted with C<_> and upper case sequence would be lower cased.
75 Example: C<fooBar> would be C<foo_bar>.
76
77 =item All other upper case characters would be lower cased.
78 Examples: C<FOOs> would be C<foos>, C<_Foo> would be C<_foo>.
79
80 =back
81
82 =head1 CAVEATS
83
84 =head2 C<no capitalization __PACKAGE__;>
85
86 If you want use capitalization pragma in module and add lower case
87 API in the module itself, then you should use pragma after all subs
88 are defined.
89
90 =head1 AUTHOR
91
92 Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
93
94 This library is free software; you can redistribute it and/or modify
95 it under the same terms as Perl itself.
96
97 =head1 SEE ALSO
98
99 L<Symbol::Approx::Sub>
100
101 =cut
Note: See TracBrowser for help on using the browser.