blib/lib/namespace.pm untested

filter: Statementstmt 0.0% 0 / 53 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 126 CC 17 SCAR 53.9
CC
17
Cov
0%
import
272.0
unimport
6.0
BEGIN
2.0
CRAP 218.4
1 package namespace;
2  
3 =head1 NAME
4  
5 namespace - Perl pragma to use like C++ namespace aliasing
6  
7 =head1 SYNOPSIS
8  
9 use namespace File => IO::File;
10  
11 my $fh = new File "foo", O_CREAT|O_WRONLY;
12 if( defined $fh )
13 {
14 print $fh "bar\n";
15 $fh->close;
16 }
17  
18 =head1 DESCRIPTION
19  
20 Allow aliasing namespace. May be useful for reusability increase.
21  
22 use namespace ALIAS => PACKAGE
23 [, qw/IMPORT_LIST [ ::SUBPACKAGE [ IMPORT_LIST ]] /];
24  
25 ALIAS and PACKAGE is required parameters;
26 IMPORT_LIST is the usual list of import.
27  
28  
29 Also may be undefined namespace and they subnamespaces:
30  
31 no namespace ALIAS;
32  
33  
34 If ALIAS begin with '::', then alias will be expandet to
35 caller namespace. If following example of pragma namespace
36 called from main:: module, then alias will be expandet to main::ALIAS::.
37
38 use namespace ::ALIAS => PACKAGE
39
40  
41 =head1 EXAMPLES
42  
43 EXAMPLE 1
44  
45 use namespace DOM => XML::DOM, qw/$VERSION ::Document $VERSION/;
46  
47 # DOM is alias for XML::DOM
48 # $VERSION from XML::DOM will be imported to DOM
49 #
50 # ::Document subpackage of XML::DOM will be aliased to DOM::Document
51 # $VERSION from XML::DOM::Document will be imported to DOM::Document
52  
53 my $doc = new DOM::Document;
54 print "Current used DOM version is $DOM::VERSION \n";
55  
56 no namespace DOM;
57  
58 # namespace DOM and all subnamespaces will be destroyed
59  
60  
61  
62 EXAMPLE 2
63  
64 use namespace DOM => XML::DOM, qw/::Document/;
65 # or
66 # use namespace DOM => XML::Sablotron::DOM, qw/:constants ::Document/;
67  
68 my $doc = new DOM::Document;
69 print "Constant 'TEXT_NODE' = ", TEXT_NODE;
70  
71 =head1 AUTHOR
72  
73 Albert MICHEEV <amichauer@cpan.org>
74  
75 =cut
76  
77 use strict qw/subs vars/;
78 $namespace::VERSION = '0.05';
79  
80  
81 sub import{
82 my ($slf, $als, $pkg) = (shift, shift, shift);
83 my $clr = (caller)[0];
84  
85 $als = $clr.$als if substr($als, 0, 2) eq '::';
86  
87 die "Package '$als' already defined!" if defined %{$als.'::'};
88  
89 require join( '/', split '::', $pkg ) . '.pm' unless defined %{$pkg.'::'};
90 @{$als.'::ISA'} = $pkg;
91  
92 if( @_ and $_[0] eq '()' ){ shift }
93 else{ unshift @_, @{$pkg.'::EXPORT'} if defined @{$pkg.'::EXPORT'} }
94  
95 my ($Pkg, $Als) = ($pkg, $als);
96  
97 while( my $imp = shift ){
98 if( substr($imp, 0, 2) eq '::' ){
99 $Pkg = $pkg.$imp;
100 $Als = $als.$imp;
101 @{$Als.'::ISA'} = $Pkg;
102 if( @_ and $_[0] eq '()' ){ shift }
103 else{ unshift @_, @{$Pkg.'::EXPORT'} if defined @{$Pkg.'::EXPORT'} }
104 }
105 elsif( $imp =~ /^:(.+)$/ ){
106 die "Can't find '$imp' export tag in $Pkg!\n" unless
107 defined ${$Pkg.'::'}{EXPORT_TAGS}{$1};
108 unshift @_, @{ ${$Pkg.'::'}{EXPORT_TAGS}{$1} };
109 }
110 elsif( $imp =~ /^([\$%@])?(.+)$/ ){
111 die "Can't find '$imp' from $Pkg!\n" unless
112 !$1 && defined \&{$Pkg.'::'.$2} or
113 $1 eq '$' && defined \${$Pkg.'::'.$2} or
114 $1 eq '@' && defined \@{$Pkg.'::'.$2} or
115 defined \%{$Pkg.'::'.$2};
116 *{$clr.'::'.$2} =
117 *{$Als.'::'.$2} =
118 !$1 ? \&{$Pkg.'::'.$2} :
119 $1 eq '$' ? \${$Pkg.'::'.$2} :
120 $1 eq '@' ? \@{$Pkg.'::'.$2} :
121 \%{$Pkg.'::'.$2};
122 }
123 else{ die "Undefined behavior!\n" }
124 }
125 }
126  
127 sub unimport{
128 undef %{(substr($_[1], 0, 2) eq '::' ? (caller)[0] : '' ).$_[1].'::'};
129 }
130  
131 1;