| 1 | # | ||||||||||||||||||
| 2 | # Copyright (c) 2006-2014 Christian Jaeger, copying@christianjaeger.ch | ||||||||||||||||||
| 3 | # | ||||||||||||||||||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||||||||||||||||||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||||||||||||||||||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||||||||||||||||||
| 7 | # bundled with this file. | ||||||||||||||||||
| 8 | # | ||||||||||||||||||
| 9 | |||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||
| 11 | |||||||||||||||||||
| 12 | Chj::Package::OfPath | ||||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | (Taken from chj-bin's perl_path2namespace.) | ||||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | =head1 NOTE | ||||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | This is alpha software! Read the status section in the package README | ||||||||||||||||||
| 24 | or on the L<website|http://functional-perl.org/>. | ||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | =cut | ||||||||||||||||||
| 27 | |||||||||||||||||||
| 28 | package Chj::Package::OfPath; | ||||||||||||||||||
| 29 | 1 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 30 | 1 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 31 | 1 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 32 | 1 | ▶ | use Cwd 'abs_path'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 33 | 1 | ▶ | use Chj::singlequote; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 34 | 1 | ▶ | use Exporter "import"; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | 1 | our @EXPORT_OK = qw( | |||||||||||||||||
| 37 | package_of_path | ||||||||||||||||||
| 38 | package_of_path_or_package | ||||||||||||||||||
| 39 | ); | ||||||||||||||||||
| 40 | |||||||||||||||||||
| 41 | 1 | our $DEBUG = 0; | |||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | 0 | ▶ | sub package_of_path { | ||||||||||||||||
|
Subroutine
package_of_path: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||
| 44 | 0 | my ($path) = @_; | |||||||||||||||||
| 45 | 0 | $path =~ s{^\./}{}; | |||||||||||||||||
| 46 | 0 | my $class = $path; | |||||||||||||||||
| 47 | 0 | $class =~ s/\.pm$//; | |||||||||||||||||
| 48 | 0 | $class =~ s|/|::|sg; | |||||||||||||||||
| 49 | 0 | ▶ | if ($path =~ m{^/}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | # absolute | ||||||||||||||||||
| 52 | } else { | ||||||||||||||||||
| 53 | 0 | ▶ | my $p = abs_path $path or die "abs_path '$path': $!"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 54 | 0 | $path = $p; | |||||||||||||||||
| 55 | } | ||||||||||||||||||
| 56 | 0 | ▶ | warn "path=" . singlequote($path) if $DEBUG; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | 0 | ▶ | open my $in, "<", $path or die "could not open '$path': $!"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | 0 | local $/; | |||||||||||||||||
| 61 | 0 | my $content = <$in>; | |||||||||||||||||
| 62 | 0 | ▶ | close $in or die "closing '$path': $!"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 63 | CHECK: { | ||||||||||||||||||
| 64 | 0 | ▶ | while ($content =~ m{\bpackage +([\w:]+)}g) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 65 | 0 | my $namespace = $1; | |||||||||||||||||
| 66 | 0 | ▶ | if ($class =~ m/\Q$namespace\E$/) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 67 | 0 | ▶ | warn "cutting '$class' down to '$namespace'\n" if $DEBUG; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 68 | 0 | $class = $namespace; | |||||||||||||||||
| 69 | 0 | last CHECK; | |||||||||||||||||
| 70 | } | ||||||||||||||||||
| 71 | } | ||||||||||||||||||
| 72 | 0 | die "could not find any package definition in '$path' " | |||||||||||||||||
| 73 | . "matching its path"; | ||||||||||||||||||
| 74 | } | ||||||||||||||||||
| 75 | $class | ||||||||||||||||||
| 76 | 0 | } | |||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | 0 | ▶ | sub package_of_path_or_package { | ||||||||||||||||
|
Subroutine
package_of_path_or_package: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||
| 79 | 0 | my ($path_or_package) = @_; | |||||||||||||||||
| 80 | 0 | ▶ | if ($path_or_package =~ m{(\S+\.pm)}) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 81 | 0 | package_of_path($1) | |||||||||||||||||
| 82 | } elsif ($path_or_package =~ m{^(\w+\:\:)*\w+\z}s) { | ||||||||||||||||||
| 83 | 0 | $path_or_package | |||||||||||||||||
| 84 | } elsif ($path_or_package =~ m{^(\w+/)*\w+\z}s) { | ||||||||||||||||||
| 85 | 0 | $path_or_package =~ s|/|::|sg; | |||||||||||||||||
| 86 | 0 | $path_or_package | |||||||||||||||||
| 87 | } else { | ||||||||||||||||||
| 88 | 0 | die "doesn't look sane: " . singlequote($path_or_package) | |||||||||||||||||
| 89 | } | ||||||||||||||||||
| 90 | } | ||||||||||||||||||
| 91 | |||||||||||||||||||
| 92 | 1 | 1 | |||||||||||||||||