File Coverage

blib/lib/mem.pm
Criterion Covered Total %
statement 10 10 100.0
branch 5 8 62.5
condition 3 4 75.0
subroutine 2 2 100.0
pod n/a
total 20 24 83.3


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3 1   50 1   665 BEGIN { require $_.".pm" && $_->import for qw(strict warnings) }
4              
5             =encoding utf-8
6              
7             =pod
8              
9             =head1 NAME
10              
11             =over
12              
13             •ḟmem - use modules in "mem"ory (already declared in same file)
14              
15             =back
16              
17             =head1 VERSION
18              
19             Version "0.4.7"
20              
21             =cut
22              
23             package mem;
24             our $VERSION='0.4.7';
25            
26             # RCS $Revision: 1.8 $ $Date: 2015-06-30 01:36:59-07 $
27             # 0.4.7 - minor POD corrections
28             # 0.4.6 - conditionalize warning & strict based on presence;
29             # - Documentation changes.
30             # 0.4.5 - Add alt version format for ExtMM
31             # 0.4.4 - Add dep on recent ExtMM @ in BUILD_REQ
32             # Documentation enhancements and clarifications.
33             # 0.4.3 - change format of VERSION to a string (vec unsupported
34             # in earlier perl versions)
35             # 0.4.2 - doc change & excisement of a symlink (maybe winprob)
36             # 0.4.1 - revert attempt to use win32 BS -- seems to cause
37             # more problems than it fixed.
38             # 0.4.0 - Documentation upgrade;
39             # Attempt to point to win32 paths w/backslash
40             # 0.3.3 - Switch to using ptar for archive creation
41             # 0.3.2 - Fix summary to be more descriptive
42             # 0.3.1 - Fix Manifest => MANIFEST
43             # 0.3.0 - Initial external 'non'-release
44              
45             sub sep_detect() { '/' }
46              
47             our $sep;
48              
49             sub import {
50 3 50   3   364 if (@_ >= 1) {
51 3         12 my ($p, $f, $l)=caller;
52 3   100     20 $sep ||= sep_detect();
53 3 50       9 if (@_ >= 1) {
54 3 50       8 $p="main" unless $p;
55 3         11 $p =~ s!::!$sep!ge;
  2         7  
56 3         6 $p .= ".pm";
57 3 100       152 $::INC{$p} = $f."#".$l unless exists $::INC{$p};
58             }
59             }
60             }
61             1;
62              
63              
64             ##########################################################################
65             # use mem; {{{1
66              
67              
68             =head1 SYNOPSIS
69              
70              
71             use mem;
72             use mem(@COMPILE_TIME_DEFINES=qw(a b c));
73              
74             B> is a I C that allows C-ing a C as it is previously defined, B. This allows easy declaration of specialized, typed data structures (like I C definitions) that increase code legibility and concept clarity. In a similar usage, the L pragma allows defining low-overhead, runtime constants.
75              
76             Allowing C of packages in the same file allows calling code to access classes in a clean, object oriented manner, allowing for identical code to
77             exist either in the same file, or in a separate file without making code
78             changes or requiring use of non-portable, language specific features to
79             accomplish the same thing.
80              
81             In the 2nd form, it can allow in-lined BEGIN's for single line
82             assignments. While one could use it as a replacement for multiple
83             lines, an actual BEGIN block can often look as much or more tidy.
84              
85             In many cases, these compile time assignments are essential to take full
86             advantage of perl's strengths. For example, without compile time assignment
87             of '@EXPORT', you can't use perl's function prototypes. Due the overhead and difficulty in getting them right, new perl programmers are dissuaded from
88             using such featues.
89              
90             When used to force assignments into the early parsing stages of perl, Using dynamically allocated, pre-initialized and type-checked data structures become
91             possible.
92              
93             =head1 EXAMPLE
94              
95             Following, is a sample program, showing two uses of C . This first example allows declaring a run-time keyword 'ARRAY', that can check to see
96             if it's argument is an ARRAY reference, B provide a runtime
97             literal, C , that can be used without quotes.
98              
99             use strict; use warnings;
100              
101             { package Ar_Type;
102             #
103             use mem; #1st usage
104             our (@EXPORT);
105             sub ARRAY (;*) {
106             my $p = $_[0]; my $t="ARRAY";
107             return @_ ? (ref $p && (1+index($p, $t))) : $t;
108             }
109             #
110             use mem( @EXPORT=qw(ARRAY) ); #2nd usage
111            
112             use Xporter;
113             }
114              
115             package main;
116             use Ar_Type;
117             use P;
118              
119             my @a=(1,2,3);
120             my ($ed, $light);
121             (@$ed, @$light) = (@a, @a); #ed & light point to copies of @a
122             bless $ed, "bee";
123              
124             P "\@a = ref of array" if ARRAY \@a;
125             P "ref of \$ed is \"%s\".", ref $ed;
126             P "ed still points to underlying type, 'array'" if ARRAY $ed;
127             P "Is ref \$light, an ARRAY?: %s", (ref $light eq ARRAY) ? 'yes':'no';
128             P "Does \"ref \$ed\" eq ARRAY?: %s", (ref $ed eq ARRAY) ? 'yes':'no';
129             P "%s", "# (Because \"ref \$ed\" is really a bless \"ed\" bee)"
130              
131             =over
132              
133             =item
134              
135             Now, to show what happens using C, and the errors that occur if you
136             do not. First, the correct output:
137              
138             @a = ref of array
139             ref of $ed is "bee".
140             ed still points to underlying type, 'array'
141             Is ref $light, an ARRAY?: yes
142             Does ref $ed eq ARRAY?: no
143             # (Because ref "ed" is really a bless"ed" bee)
144              
145              
146             =item
147              
148             Second, B> the first "C< use mem >", presuming the line was commented out:
149              
150             Can't locate Ar_Type.pm in @INC (@INC contains:
151             /usr/lib/perl5/5.18.2 ... /usr/lib/perl5/site_perl .)
152             at /tmp/ex line 18.
153             BEGIN failed--compilation aborted at /tmp/ex line 18.
154              
155             This is due to C, the package already declared
156             and in Iory>>, being I by Perl's C statement
157             because some I, I<"internal flag"> is not set for
158             C. The first C causes this flag, normally
159             set with the path of the of a Cd file, to be set with the
160             containing file path and an added comment, containing the line number.
161              
162             This tells perl to use the definition of the package that is already
163             in Cory.
164              
165             =over
166              
167             I
168              
169             =back
170              
171             =item
172              
173             Third, instead of dropping the 1st "C< use mem >", you drop (or comment out) the 2nd usage in the above example, you get:
174              
175             Bareword "ARRAY" not allowed while "strict subs"
176             in use at /tmp/ex line 27.
177             syntax error at /tmp/ex line 27, near "ARRAY \"
178             Bareword "ARRAY" not allowed while "strict subs"
179             in use at /tmp/ex line 30.
180             Bareword "ARRAY" not allowed while "strict subs"
181             in use at /tmp/ex line 31.
182             Execution of /tmp/ex aborted due to compilation errors.
183              
184              
185             This happens because when C is called, the
186             contents of C<@EXPORT> is not known. Even with the assignment
187             to C<@EXPORT>, the "C<@EXPORT=qw(ARRAY)>" being right above
188             the C statement. Similarly to the first error, above,
189             Perl doesn't use the value of C<@EXPORT> just above it. Having
190             C< use mem > in the second position forces Perl to put the assignment
191             to @EXPORT in C< mem >ory, so that when C< use Exporter > is called,
192             it can pick up the name of C as already being "exported" and
193             B.
194              
195             Without C putting the value of C<@EXPORT> in Cory,
196             C isn't defined, an you get the errors shown above.
197              
198             =back
199              
200             =head2 Summary
201              
202             The first usage allows 'C
' to find C, I
203             Cory>.
204              
205             The second usage forces the definition of 'C' into Cory so
206             they can be exported by an exporter function.
207              
208             In B cases, C allows your already-in-Cory code to
209             be used. Thsi allows simplified programming and usage without knowledge
210             of or references to Perl's internal-flags or internal run phases.
211              
212              
213             =head1 SEE ALSO
214              
215             See L for more help with exporting features from your modules, or
216             the older L for the cadillac of exporting that will do everything you want (and a bit more). See L

for more details about

217             the generic print operator that is actually B, and see L for a more complete treatment of the CORE Types (with helpers for other perl data types besides C's.
218              
219              
220              
221             =cut
222