Linda A Walsh > mem-0.4.6 > •ƒmem


Annotate this POD


Open  0
View/Report Bugs


•ƒmem - use modules from "mem"ory & allow consistent syntax for inits


Version "0.4.6"


  use 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 allows compile-time assignments to variables in a way that smoothly integrates needed assignments into header blocks without disrupting the layout and flow of the code with the increased visual clutter added by BEGIN blocks that have their own syntax.

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.


Following, 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(                                    #2nd usage 
      )                                           #(also) 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)"


The first usage allows 'main' to find package Ar_Type, already in memory.

The second usage forces the definition of 'ARRAY' into memory so they can be exported by an exporter function.

In both cases, mem allows your already-in-memory 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 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.

syntax highlighting: