Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

capitalization.pm

by miyagawa (Chaplain)
on Feb 06, 2002 at 23:00 UTC ( #143734=sourcecode: print w/replies, xml ) Need Help??
Category: Fun
Author/Contact Info miyagawa tarball
Description: no capitalization on method names
package capitalization;

use strict;
use vars qw($VERSION);
$VERSION = 0.01;

use Devel::Symdump;

my %done;

sub unimport {
    my($class, @mods) = @_;
    for my $mod (@mods) {
    next if $done{$mod};

    my $file = mod2file($mod);
    require $file unless $INC{$file};

    my $dump = Devel::Symdump->new($mod);
    for my $meth (map { s/^\Q$mod\E:://; $_ } $dump->functions) {
        my $new = nocap($meth);
        if ($new ne $meth) {
        no strict 'refs';
        *{"$mod\::$new"} = \&{"$mod\::$meth"};
        }
    }
    $done{$mod} = 1;
    }
}

sub nocap {
    my $method = shift;
    $method =~ s/(?<=[a-z])([A-Z]+)/"_" . lc($1)/eg;
    lcfirst $method;
}

sub mod2file {
    my $mod = shift;
    $mod =~ s!::!/!g;
    return "$mod.pm";
}

1;
__END__

=head1 NAME

capitalization - no capitalization on method names

=head1 SYNOPSIS

  use XML::DOM;
  no capitalization 'XML::DOM';

  my $parser = XML::DOM::Parser->new;

  # no capitalization ..
  my $nodes = $parser->get_elements_by_tag_name("Foo");

  # this can be OK
  my $nodes = $parser->getElementsByTagName("Foo");


=head1 DESCRIPTION

capitalization.pm allows you to use familiar style on method naming.

=head1 AUTHOR

Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=head1 SEE ALSO

L<Symbol::Approx::Sub>

=cut
Replies are listed 'Best First'.
Re: capitalization.pm
by dash2 (Hermit) on Feb 12, 2002 at 15:28 UTC
    Cute. Your mod2file method is unportable though... you could probably use one of the File::Spec functions to get a portable file separator.

    dave hj~

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://143734]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2023-10-04 19:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?