| 1 | package Class::ModuleByFile; ## Can read the module name from file | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 4 | ▶ | use FileHandle; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 4 | ▶ | use vars qw(@ISA @EXPORT %EXPORT_TAGS $VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 4 | our $VERSION = '0.02'; | |||||||||
| 9 | |||||||||||
| 10 | 4 | ▶ | use Exporter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 4 | @ISA = qw(Exporter); | |||||||||
| 13 | |||||||||||
| 14 | 4 | %EXPORT_TAGS = ( all => [qw( | |||||||||
| 15 | get_name | ||||||||||
| 16 | get_module_by_file | ||||||||||
| 17 | get_parent | ||||||||||
| 18 | )] ); | ||||||||||
| 19 | |||||||||||
| 20 | 4 | Exporter::export_ok_tags('all'); | |||||||||
| 21 | |||||||||||
| 22 | # You can use that class to get the module name (package) by filename. | ||||||||||
| 23 | # It reads the file and returns the package entry. | ||||||||||
| 24 | # | ||||||||||
| 25 | # | ||||||||||
| 26 | # SYNOPSIS | ||||||||||
| 27 | # ======== | ||||||||||
| 28 | # | ||||||||||
| 29 | # use Class::ModuleByFile; | ||||||||||
| 30 | # print Class::ModuleByFile::get_name('/tmp/Local/Bar.pm'); | ||||||||||
| 31 | # # may print 'Local::Bar' | ||||||||||
| 32 | # | ||||||||||
| 33 | # use Class::ModuleByFile 'get_module_by_file'; | ||||||||||
| 34 | # # imports the function get_module_by_file() | ||||||||||
| 35 | # # to use it directly. | ||||||||||
| 36 | # | ||||||||||
| 37 | # LICENSE | ||||||||||
| 38 | # ======= | ||||||||||
| 39 | # You can redistribute it and/or modify it under the conditions of LGPL. | ||||||||||
| 40 | # | ||||||||||
| 41 | # AUTHOR | ||||||||||
| 42 | # ====== | ||||||||||
| 43 | # Andreas Hernitscheck ahernit(AT)cpan.org | ||||||||||
| 44 | |||||||||||
| 45 | |||||||||||
| 46 | |||||||||||
| 47 | |||||||||||
| 48 | |||||||||||
| 49 | |||||||||||
| 50 | |||||||||||
| 51 | # Returns the modulename for the given file. | ||||||||||
| 52 | # It reads the text of the file and returns | ||||||||||
| 53 | # the package entry | ||||||||||
| 54 | 2 | ▶ | sub get_name { # $modulename ($filename) | ||||||||
|
Subroutine
get_name: called CC=6 SCAR=20.5
Pod
documented
| |||||||||||
| 55 | 2 | ▶ | my $file = shift or die 'requires filename'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | |||||||||||
| 57 | 0 | ▶ | if (!-e $file){die "file $file does not exist"}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | |||||||||||
| 59 | 2 | my $fh = FileHandle->new($file, "r"); | |||||||||
| 60 | 2 | ▶ | if (defined $fh) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 2 | ▶ | while (my $line = <$fh>){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | |||||||||||
| 63 | 2 | ▶ | if ( $line=~ m/\s*package\s+([^ ;]+)/ ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 2 | return $1; | |||||||||
| 65 | } | ||||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 0 | $fh->close(); | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | |||||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | # alias for get_name() | ||||||||||
| 75 | 1 | ▶ | sub get_module_by_file { | ||||||||
|
Subroutine
get_module_by_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 76 | 1 | return get_name(@_); | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | |||||||||||
| 80 | # If Moose is used and extend to set a parent, | ||||||||||
| 81 | # it reads that entry. | ||||||||||
| 82 | 1 | ▶ | sub get_parent { | ||||||||
|
Subroutine
get_parent: called CC=6 SCAR=19.6
Pod
undocumented
| |||||||||||
| 83 | 1 | ▶ | my $file = shift or die 'requires filename'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | |||||||||||
| 85 | 0 | ▶ | if (!-e $file){die "file $file does not exist"}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | |||||||||||
| 87 | 1 | my $fh = FileHandle->new($file, "r"); | |||||||||
| 88 | 1 | ▶ | if (defined $fh) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 1 | ▶ | while (my $line = <$fh>){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | |||||||||||
| 91 | 3 | ▶ | if ( $line=~ m/\s*extends\s+['"]([^ '"]+)['"]/ ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 1 | return $1; | |||||||||
| 93 | } | ||||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | 0 | $fh->close(); | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | |||||||||||
| 102 | |||||||||||
| 103 | |||||||||||
| 104 | 4 | 1; | |||||||||
| 105 | |||||||||||
| 106 | #################### pod generated by Pod::Autopod - keep this line to make pod updates possible #################### | ||||||||||
| 107 | |||||||||||
| 108 | =head1 NAME | ||||||||||
| 109 | |||||||||||
| 110 | Class::ModuleByFile - Can read the module name from file | ||||||||||
| 111 | |||||||||||
| 112 | |||||||||||
| 113 | =head1 SYNOPSIS | ||||||||||
| 114 | |||||||||||
| 115 | |||||||||||
| 116 | use Class::ModuleByFile; | ||||||||||
| 117 | print Class::ModuleByFile::get_name('/tmp/Local/Bar.pm'); | ||||||||||
| 118 | # may print 'Local::Bar' | ||||||||||
| 119 | |||||||||||
| 120 | use Class::ModuleByFile 'get_module_by_file'; | ||||||||||
| 121 | # imports the function get_module_by_file() | ||||||||||
| 122 | # to use it directly. | ||||||||||
| 123 | |||||||||||
| 124 | |||||||||||
| 125 | |||||||||||
| 126 | =head1 DESCRIPTION | ||||||||||
| 127 | |||||||||||
| 128 | You can use that class to get the module name (package) by filename. | ||||||||||
| 129 | It reads the file and returns the package entry. | ||||||||||
| 130 | |||||||||||
| 131 | |||||||||||
| 132 | |||||||||||
| 133 | |||||||||||
| 134 | =head1 REQUIRES | ||||||||||
| 135 | |||||||||||
| 136 | L<Exporter> | ||||||||||
| 137 | |||||||||||
| 138 | L<FileHandle> | ||||||||||
| 139 | |||||||||||
| 140 | L<5.006> | ||||||||||
| 141 | |||||||||||
| 142 | |||||||||||
| 143 | =head1 METHODS | ||||||||||
| 144 | |||||||||||
| 145 | =head2 get_module_by_file | ||||||||||
| 146 | |||||||||||
| 147 | get_module_by_file(); | ||||||||||
| 148 | |||||||||||
| 149 | alias for get_name() | ||||||||||
| 150 | |||||||||||
| 151 | |||||||||||
| 152 | =head2 get_name | ||||||||||
| 153 | |||||||||||
| 154 | my $modulename = get_name($filename); | ||||||||||
| 155 | |||||||||||
| 156 | Returns the modulename for the given file. | ||||||||||
| 157 | It reads the text of the file and returns | ||||||||||
| 158 | the package entry | ||||||||||
| 159 | |||||||||||
| 160 | |||||||||||
| 161 | |||||||||||
| 162 | =head1 AUTHOR | ||||||||||
| 163 | |||||||||||
| 164 | Andreas Hernitscheck ahernit(AT)cpan.org | ||||||||||
| 165 | |||||||||||
| 166 | |||||||||||
| 167 | =head1 LICENSE | ||||||||||
| 168 | |||||||||||
| 169 | You can redistribute it and/or modify it under the conditions of LGPL. | ||||||||||
| 170 | |||||||||||
| 171 | |||||||||||
| 172 | |||||||||||
| 173 | =cut | ||||||||||
| 174 | |||||||||||