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

for more details about

216             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.
217              
218              
219              
220             =cut
221