|
NAME•ḟmem - use modules in "mem"ory
(already declared in same file)
VERSIONVersion "0.4.7"SYNOPSISuse mem; use mem(@COMPILE_TIME_DEFINES=qw(a b c)); "mem" is a syntactic-sugar "pragma" that allows "use"-ing a "package" as it is previously defined, in the same file. This allows easy declaration of specialized, typed data structures (like C "struct" definitions) that increase code legibility and concept clarity. In a similar usage, the constants pragma allows defining low-overhead, runtime constants. Allowing "use" of packages in the same file allows calling code to access classes in a clean, object oriented manner, allowing for identical code to exist either in the same file, or in a separate file without making code changes or requiring use of non-portable, language specific features to accomplish the same thing. In the 2nd form, it can allow in-lined BEGIN's for single line assignments. While one could use it as a replacement for multiple lines, an actual BEGIN block can often look as much or more tidy. In many cases, these compile time assignments are essential to take full advantage of perl's strengths. For example, without compile time assignment 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 using such featues. When used to force assignments into the early parsing stages of perl, Using dynamically allocated, pre-initialized and type-checked data structures become possible. EXAMPLEFollowing, is a sample program, showing two uses of "mem" . This first example allows declaring a run-time keyword 'ARRAY', that can check to see if it's argument is an ARRAY reference, and provide a runtime literal, "ARRAY" , that can be used without quotes.use strict; use warnings; { package Ar_Type; # use mem; #1st usage our (@EXPORT); sub ARRAY (;*) { my $p = $_[0]; my $t="ARRAY"; return @_ ? (ref $p && (1+index($p, $t))) : $t; } # use mem( @EXPORT=qw(ARRAY) ); #2nd usage use Xporter; } package main; use Ar_Type; use P; 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, an 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)"
and
SummaryThe 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 ALSOSee Xporter for more help with exporting features from your modules, or the older Exporter for the cadillac of exporting that will do everything you want (and a bit more). See P for more details about the generic print operator that is actually user friendly, and see Types::Core for a more complete treatment of the CORE Types (with helpers for other perl data types besides "ARRAY"'s.
Visit the GSP FreeBSD Man Page Interface. |