NAME
mem - use "in-mem" pkgs & force definitions into mem early
VERSION
Version "0.4.4"
SYNOPSIS
use mem;
use mem(@COMPILE_TIME_DEFINES=qw(a b c));
mem
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 use
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.
With parameter assignments or other actions, it forces those assignments to be 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.
EXAMPLE
Following, is a sample program, showing two uses of mem
.
use strict; use warnings;
{ package Ar_Type;
#
use mem; #1st usage
our (@EXPORT, @ISA);
sub ARRAY (;*) {
my $p = $_[0]; my $t="ARRAY";
return @_ ? (ref $p && (1+index($p, $t))) : $t;
}
#
use mem( #2nd usage
@EXPORT=qw(ARRAY), @ISA=qw(Exporter)
#
) #(also) 2nd usage
;
use Exporter;
}
package main;
use Ar_Type;
use P;
use Types::Core
my @a=(1,2,3);
my ($ed, $light);
(@$ed, @$light) = (@a, @a); #ed & light point to copies of @a
bless $ed, "bee";
P "\@a = ref of array" if ARRAY \@a;
P "ref of ed is %s", ref $ed;
P "ed still points to underlying type, 'array'" if ARRAY $ed;
P "Is ref \$light, ARRAY?: %s", (ref $light eq ARRAY) ? 'yes':'no';
P "Does \"ref \$ed\" eq ARRAY?: %s", (ref $ed eq ARRAY) ? 'yes':'no';
P "%s", "# (Because \"ref \$ed\" is really a bless \"ed\" bee)"
First, the correct output:
@a = ref of array ref of ed is bee ed still points to underlying type, 'array' Is ref $light, ARRAY?: yes Does ref $ed eq ARRAY?: no # (Because ref "ed" is really a bless "ed" bee)
Second, without the first "
use mem
", presuming the line was commented out:Can't locate Ar_Type.pm in @INC (@INC contains: /usr/lib/perl5/5.16.2 ... /usr/lib/perl5/site_perl .) at /tmp/ex line 18. BEGIN failed--compilation aborted at /tmp/ex line 18.
This is due to
package AR_Type
, the package already declared and inmem
ory>, being ignored by Perl'suse
statement because some Perl-specific, "internal flag" is not set forpackage Ar_Type
. The firstuse mem
causes this flag, normally set with the path of the of ause
d file, to be set with the containing file path and an added comment, containing the line number.This tells perl to use the definition of the package that is already in
mem
ory.and
Third, instead of dropping the 1st "
use mem
", you drop (or comment out) the 2nd usage in the above example, you get:Bareword "ARRAY" not allowed while "strict subs" in use at /tmp/ex line 27. syntax error at /tmp/ex line 27, near "ARRAY \" Bareword "ARRAY" not allowed while "strict subs" in use at /tmp/ex line 30. Bareword "ARRAY" not allowed while "strict subs" in use at /tmp/ex line 31. Execution of /tmp/ex aborted due to compilation errors.
This happens because when
use Exporter
is called, the contents of@EXPORT
is not known. Even with the assignment to@EXPORT
, the "@EXPORT=qw(ARRAY)
" being right above theuse Exporter
statement. Similarly to the first error, above, Perl doesn't use the value of@EXPORT
just above it. Havinguse mem
in the second position forces Perl to put the assignment to @EXPORT inmem
ory, so that whenuse Exporter
is called, it can pick up the name ofARRAY
as already being "exported" and defined.Without
use mem
putting the value of@EXPORT
inmem
ory,ARRAY
isn't defined, an you get the errors shown above.
Summary
The first usage allows 'main
' to find package Ar_Type
, already in mem
ory.
The second usage forces the definition of 'ARRAY
' into mem
ory so they can be exported by an exporter function.
In both cases, mem
allows your already-in-mem
ory code to be used. Thsi allows simplified programming and usage without knowledge of or references to Perl's internal-flags or internal run phases.
SEE ALSO
See Exporter for information on exporting names. See the newer, Xporter for doing similar without the need for setting @ISA
and persistent defaults in @EXPORT
. See P for more details about the generic print verb and see Types::Core for a more complete treatment of the CORE Types.