Data::PackageName - OO handling of package name transformations
0.01
use Data::PackageName; my $foo = Data::PackageName->new('Foo'); print "$foo\n"; # prints 'Foo' my $foo_bar = $foo->append('Bar'); print "$foo_bar\n"; # prints 'Foo::Bar' my $quuxbaz_foo_bar = $foo_bar->prepend('QuuxBaz'); print "$quuxbaz_foo_bar\n"; # prints 'QuuxBaz::Foo::Bar' my $bar = $quuxbaz_foo_bar->after_start(qw( QuuxBaz )); print "$bar\n"; # prints 'Bar' # prints QuuxBaz/Foo/Bar print join('/', $quuxbaz_foo_bar->parts), "\n"; # prints quux_baz/foo/bar print join('/', $quuxbaz_foo_bar->parts_lc), "\n"; # create a Path::Class::File and a Path::Class::Dir my $file = $quuxbaz_foo_bar->filename('.yml'); my $dir = $quuxbaz_foo_bar->dirname; print "$file\n"; # prints quux_baz/foo/bar.yml print "$dir\n"; # prints quux_baz/foo/bar
This module provides the mostly simple functionality of transforming package names in common ways. I didn't write it because it is complicated, but rather because I have done it once too often.
Data::PackageName is a Moose class.
Data::PackageName
A Str representing the package name, e.g. Foo::Bar. This attribute is required and must be specified at creation time.
Str
Foo::Bar
This method is inherited from Moose and only referenced here for completeness. Please consult the Moose documentation for a complete description of the object model.
my $foo_bar = Data::PackageName->new(package => 'Foo::Bar');
The "package" attribute is required.
This method is imported from Moose and only referenced here for completeness. Please consult the Moose documentation for a complete description of the object model.
The meta method returns the Moose meta class.
meta
# Foo::Bar::Baz my $foo_bar_baz = $foo_bar->append('Baz'); # Foo::Bar::Baz::Qux my $foo_bar_baz_qux = $foo_bar->append('Baz::Qux'); # same as above my $foo_bar_baz_qux2 = $foo_bar->append(qw( Baz Qux ));
This method returns a new Data::PackageName instance with its arguments appended as name parts. This means that qw( Foo Bar ) is equivalent to Foo::Bar.
qw( Foo Bar )
Does the same as "append", but rather than appending its arguments it prepends the new package with them.
You often want to get to the part of a module name that is under a specific namespace, for example to remove the project's root namespace from the front.
my $p = Data::PackageName->new(package => 'MyProject::Foo::Bar'); print $p->after_start('MyProject'), "\n"; # prints 'Foo::Bar'
This method accepts values exactly as "append" and "prepend" do. The argument list will be joined with :: as separator, so it doesn't matter how you pass the names in.
::
This splits up the namespace in parts.
my $p = Data::PackageName->new(package => 'Foo::Bar::Baz'); print join(', ', $p->parts), "\n"; # prints 'Foo, Bar, Baz'
This module uses a simple algorithm to transform namespace parts into their lowercase representations. For example, Foo would of course become foo, but FooBar would result in foo_bar.
Foo
foo
FooBar
foo_bar
# prints 'foo' print Data::PackageName->transform_to_lc('Foo'), "\n"; # prints 'foo_bar' print Data::PackageName->transform_to_lc('FooBar'), "\n";
The same as "parts", but each part will be transformed to lowercase with "transform_to_lc" first.
This returns a Path::Class::File object with a path containing the lower-cased parts of the package name.
# prints 'foo/bar_baz' my $p = Data::PackageName->new(package => 'Foo::BarBaz'); print $p->filename_lc, "\n";
You can optionally specify a file extension that will be appended to the filename.
# prints 'foo/bar_baz.yml' my $p = Data::PackageName->new(package => 'Foo::BarBaz'); print $p->filename_lc('.yml'), "\n";
Returns a Path::Class::Dir object containing the lower-cased parts of the package name.
# prints 'foo/bar' my $p = Data::PackageName->new(package => 'Foo::Bar'); print $p->dirname, "\n";
This will return a Path::Class::File object containing the filename the package corresponds to, e.g. Foo::Bar would be an object with the value Foo/Bar.pm.
Path::Class::File
Foo/Bar.pm
This will try to load the package via Perl's require builtin. It will return true if it loaded the file, false if it was already loaded. Exceptions raised by require will not be intercepted.
require
Returns true if the package is already loaded, false if it's not.
Moose (Underlying object system), Path::Class ("filename_lc" and "dirname" methods)
Moose (Underlying object system), Scalar::Util (blessed for object recreation), Path::Class::File (Filenames), Path::Class::Dir (Dirnames), Class::Inspector ("package_filename" transition and loaded-class detection)
blessed
Robert 'phaylon' Sedlacek <rs@474.at>
<rs@474.at>
This program is free software; you can redistribute it and/or modify it under the same terms as perl itself.
To install Data::PackageName, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::PackageName
CPAN shell
perl -MCPAN -e shell install Data::PackageName
For more information on module installation, please visit the detailed CPAN module installation guide.