File Coverage

blib/lib/mem.pm
Criterion Covered Total %
statement 12 12 100.0
branch 5 8 62.5
condition 2 2 100.0
subroutine 2 2 100.0
pod n/a
total 21 24 87.5


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -w
2 1     1   639 use strict;
  1         2  
  1         294  
3             #does nothing if run!
4             #
5              
6             package mem;
7             our $VERSION='0.4.5';
8            
9             our %os2sep = ( Wi => "\x{5c}", MS => "\x{5c}" );
10              
11             sub sep_detect() {
12             # my $OS = substr(($ENV{OS} || $^O),0,2);
13             # defined $os2sep{$OS} ? $os2sep{$OS} :
14             '/';
15             }
16              
17             # RCS $Revision: 1.7 $ $Date: 2013-12-16 13:21:47-08 $
18             # 0.4.5 - Add alt version format for ExtMM
19             # 0.4.4 - Add dep on recent ExtMM @ in BUILD_REQ
20             # Documentation enhancements and clarifications.
21             # 0.4.3 - change format of VERSION to a string (vec unsupported
22             # in earlier perl versions)
23             # 0.4.2 - doc change & excisement of a symlink (maybe winprob)
24             # 0.4.1 - revert attempt to use win32 BS -- seems to cause
25             # more problems than it fixed.
26             # 0.4.0 - Documentation upgrade;
27             # Attempt to point to win32 paths w/backslash
28             # 0.3.3 - Switch to using ptar for archive creation
29             # 0.3.2 - Fix summary to be more descriptive
30             # 0.3.1 - Fix Manifest => MANIFEST
31             # 0.3.0 - Initial external 'non'-release
32              
33             our $sep;
34              
35             sub import {
36 3 50   3   270 if (@_ >= 1) {
37 3         13 my ($p, $f, $l)=caller;
38 3   100     13 $sep ||= sep_detect();
39 3 50       9 if (@_ >= 1) {
40 3 50       7 $p="main" unless $p;
41 3         12 $p =~ s!::!$sep!ge;
  2         6  
42 3         6 $p .= ".pm";
43 3 100       176 $::INC{$p} = $f."#".$l unless exists $::INC{$p};
44             }
45             }
46             }
47             1;
48              
49              
50             ##########################################################################
51             # use mem; {{{1
52              
53             =pod
54              
55             =head1 NAME
56              
57             =over
58              
59             =item
60              
61             mem - use "in-mem" pkgs & force definitions into mem early
62              
63             =back
64              
65             =head1 VERSION
66              
67             =over
68              
69             Version "0.4.5"
70              
71             =back
72              
73             =head1 SYNOPSIS
74              
75              
76             use mem;
77             use mem(@COMPILE_TIME_DEFINES=qw(a b c));
78              
79             B> is a trivial pragma to either allow defining the module it is included from as being defined so that later classes or packages in the same file can C the package to pull in a reference to it, or to be able to call its import routine from a different package in the same file.
80              
81             With parameter assignments or other actions, it forces those assignments to be
82             done, immediately, at compile time instead of later at run time. It can be use, for example, with Exporter, to export typed-sub's among other usages.
83              
84              
85             =head1 EXAMPLE
86              
87             Following, is a sample program, showing two uses of C.
88              
89             use strict; use warnings;
90              
91             { package Ar_Type;
92             #
93             use mem; #1st usage
94             our (@EXPORT, @ISA);
95             sub ARRAY (;*) {
96             my $p = $_[0]; my $t="ARRAY";
97             return @_ ? (ref $p && (1+index($p, $t))) : $t;
98             }
99             #
100             use mem( #2nd usage
101             @EXPORT=qw(ARRAY), @ISA=qw(Exporter)
102             #
103             ) #(also) 2nd usage
104             ;
105             use Exporter;
106             }
107              
108             package main;
109             use Ar_Type;
110             use P;
111             use Types::Core
112              
113             my @a=(1,2,3);
114             my ($ed, $light);
115             (@$ed, @$light) = (@a, @a); #ed & light point to copies of @a
116             bless $ed, "bee";
117              
118             P "\@a = ref of array" if ARRAY \@a;
119             P "ref of ed is %s", ref $ed;
120             P "ed still points to underlying type, 'array'" if ARRAY $ed;
121             P "Is ref \$light, ARRAY?: %s", (ref $light eq ARRAY) ? 'yes':'no';
122             P "Does \"ref \$ed\" eq ARRAY?: %s", (ref $ed eq ARRAY) ? 'yes':'no';
123             P "%s", "# (Because \"ref \$ed\" is really a bless \"ed\" bee)"
124              
125             =over
126              
127             =item
128              
129             First, the correct output:
130              
131             @a = ref of array
132             ref of ed is bee
133             ed still points to underlying type, 'array'
134             Is ref $light, ARRAY?: yes
135             Does ref $ed eq ARRAY?: no
136             # (Because ref "ed" is really a bless "ed" bee)
137              
138              
139             =item
140              
141             Second, B> the first "C< use mem >", presuming the line was commented out:
142              
143             Can't locate Ar_Type.pm in @INC (@INC contains:
144             /usr/lib/perl5/5.16.2 ... /usr/lib/perl5/site_perl .)
145             at /tmp/ex line 18.
146             BEGIN failed--compilation aborted at /tmp/ex line 18.
147              
148             This is due to C, the package already declared
149             and in Iory>>, being I by Perl's C statement
150             because some I, I<"internal flag"> is not set for
151             C. The first C causes this flag, normally
152             set with the path of the of a Cd file, to be set with the
153             containing file path and an added comment, containing the line number.
154              
155             This tells perl to use the definition of the package that is already
156             in Cory.
157              
158             =over
159              
160             I
161              
162             =back
163              
164             =item
165              
166             Third, instead of dropping the 1st "C< use mem >", you drop (or comment out) the 2nd usage in the above example, you get:
167              
168             Bareword "ARRAY" not allowed while "strict subs"
169             in use at /tmp/ex line 27.
170             syntax error at /tmp/ex line 27, near "ARRAY \"
171             Bareword "ARRAY" not allowed while "strict subs"
172             in use at /tmp/ex line 30.
173             Bareword "ARRAY" not allowed while "strict subs"
174             in use at /tmp/ex line 31.
175             Execution of /tmp/ex aborted due to compilation errors.
176              
177              
178             This happens because when C is called, the
179             contents of C<@EXPORT> is not known. Even with the assignment
180             to C<@EXPORT>, the "C<@EXPORT=qw(ARRAY)>" being right above
181             the C statement. Similarly to the first error, above,
182             Perl doesn't use the value of C<@EXPORT> just above it. Having
183             C< use mem > in the second position forces Perl to put the assignment
184             to @EXPORT in C< mem >ory, so that when C< use Exporter > is called,
185             it can pick up the name of C as already being "exported" and
186             B.
187              
188             Without C putting the value of C<@EXPORT> in Cory,
189             C isn't defined, an you get the errors shown above.
190              
191             =back
192              
193             =head2 Summary
194              
195             The first usage allows 'C
' to find C, I
196             Cory>.
197              
198             The second usage forces the definition of 'C' into Cory so
199             they can be exported by an exporter function.
200              
201             In B cases, C allows your already-in-Cory code to
202             be used. Thsi allows simplified programming and usage without knowledge
203             of or references to Perl's internal-flags or internal run phases.
204              
205             =head1 SEE ALSO
206              
207              
208             See L for information on exporting names. See the newer,
209             L for doing similar without the need for setting C<@ISA>
210             and persistent defaults in C<@EXPORT>. See L

for more details about

211             the generic print verb and see L for a more complete
212             treatment of the CORE Types.
213              
214              
215              
216             =cut
217