++ed by:
PLU STEPHEN MIKEGRB ADAMJS CFEDDE

23 PAUSE users
26 non-PAUSE users.

Rocco Caputo

NAME

POE::Preprocessor - a macro/const/enum preprocessor

SYNOPSIS

  use POE::Preprocessor;

  # use POE::Preprocessor ( isa => 'POE::SomeModule' );

  macro max (one,two) {
    ((one) > (two) ? (one) : (two))
  }

  print {% max $one, $two %}, "\n";

  const PI 3.14159265359

  print "PI\n";  # Substitutions don't grok Perl!

  enum ZERO ONE TWO
  enum 12 TWELVE THIRTEEN FOURTEEN
  enum + FIFTEEN SIXTEEN SEVENTEEN

  print "ZERO ONE TWO TWELVE THIRTEEN FOURTEEN FIFTEEN SIXTEEN SEVENTEEN\n";

  if ($expression) {      # include
     ... lines of code ...
  }                       # include

  unless ($expression) {  # include
    ... lines of code ...
  } elsif ($expression) { # include
    ... lines of code ...
  } else {                # include
    ... lines of code ...
  }                       # include

DESCRIPTION

POE::Preprocessor is a Perl source filter that implements a simple macro substitution language. Think of it like compile-time code templates.

Macros

Macros are defined with the macro statement. The syntax is similar to Perl subs:

  macro macro_name (parameter_0, parameter_1) {
    macro code ... parameter_0 ... parameter_1 ...
  }

The open brace is required to be on the same line as the macro statement. The Preprocessor doesn't analyze macro bodies. Instead, it assumes that any closing brace in the leftmost column ends an open macro.

The parameter list is optional for macros that don't accept parameters.

  macro macro_name {
    macro code;
  }

Macros are substituted into a program with a syntax borrowed from Iaijutsu and altered slightly to jive with Perl's native syntax.

  {% macro_name $param_1, 'param two' %}

This is the code the first macro would generate:

  macro code ... $param_1 ... 'param two' ...

It's very simplistic. See POE::Kernel for extensive macro use.

Constants and Enumerations

The const command defines a constant.

  const CONSTANT_NAME    'constant value'
  const ANOTHER_CONSTANT 23

Enumerations are defined with the emun command. Enumerations start from zero by default:

  enum ZEROTH FIRST SECOND ...

If the first parameter of an enumeration is a number, then the enumerated constants will start with that value:

  enum 10 TENTH ELEVENTH TWELFTH

enum statements may not span lines. If the first enumeration parameter is a plus sign, the constants will start where a previous enum left off.

  enum 13 THIRTEENTH FOURTEENTH  FIFTEENTH
  enum +  SIXTEENTH  SEVENTEENTH EIGHTEENTH

Conditional Code Inclusion (#ifdef)

The preprocessor supports something like cpp's #if/#else/#endif by usurping a bit of Perl's conditional syntax. The following conditional statements will be evaluated at compile time if they are followed by the comment # include:

  if (EXPRESSION) {      # include
    BLOCK;
  } elsif (EXPRESSION) { # include
    BLOCK;
  } else {               # include
    BLOCK;
  }                      # include

  unless (EXPRESSION) {  # include
    BLOCK;
  }                      # include

The code in each conditional statement's BLOCK will be included or excluded in the compiled code depending on the outcome of its EXPRESSION.

Conditional includes are nestable, but else and elsif must be on the same line as the previous block's closing brace, as they are in the previous example.

Conditional includes are experimental pending a decision on how useful they are.

IMPORTING MACROS/CONSTANTS

    use POE::Preprocessor ( isa => 'POE::SomeModule' );

This method of calling Preprocessor causes the macros and constants of POE::SomeModule to be imported for use in the current namespace. These macros and constants can be overriden simply by defining items in the current namespace of the same name.

Note: if the macros in POE::SomeModule require additional perl modules, any code which imports these macros will need to use those modules as well.

DEBUGGING

POE::Preprocessor has three debugging constants which may be defined before the first time POE::Preprocessor is used.

To trace source filtering in general, and to see the resulting code and operations performed on each line:

  sub POE::Preprocessor::DEBUG () { 1 }

To trace macro invocations as they happen:

  sub POE::Preprocessor::DEBUG_INVOKE () { 1 }

To see macro, constant, and enum definitions:

  sub POE::Preprocessor::DEBUG_DEFINE () { 1 }

To see warnings when a macro or constant is redefined:

  sub POE::Preprocessor::WARN_DEFINE () { 1 }

ENVIRONMENT

Setting the POE_PREPROC_DUMP environment variable causes POE::Preprocessor to write new copies of the modules it expands. POE_PREPROC_DUMP should contain the name of a base directory. It need not exist. All the expanded files will be written under the directory in POE_PREPROC_DUMP.

Note: Some modules such as POE::Kernel alter the macros they load at compile time. Versions of these modules created by POE_PREPROC_DUMP may only be used in the same situation they were created in. For instance, a version of POE::Kernel created with Gtk support macros may only be used in Gtk programs from that point on.

Note: Because POE::Preprocessor can only dump source code from the point it it is used in a file, it should be the first statement after package Foo; in a module. Otherwise code before it will be missing from the resulting expanded file. POE::Preprocessor will create a new package Foo; line to replace the one that it could not see.

This is good:

  package Foo;  # Not seen but simulated in the expanded version.
  use POE::Preprocessor;
  use Carp;     # Seen and included in the expanded version.

This is bad:

  package Foo;  # Not seen but simulated in the expanded version.
  use Carp;     # Not seen and OMITTED FROM the expanded version.
  use POE::Preprocessor;

PERLAPP AND PERL2EXE SUPPORT

Thanks to Zoltan Kandi for assistance in testing and documenting this. This example uses a POE program, but POE::Preprocessor may be used without the rest of POE. The general ideas are the same, but the examples will be different.

POE::Preprocessor supports PerlApp (from the ActiveState PDK) and probably perl2exe as well. These perl "compilers" may require POE programs to be altered slightly to work.

Most notably, PerlApp cannot find modules imported using the POE module. That is, this will not work:

  use POE qw(A B C);

Modules must instead be used explicitly for PerlApp to include them. Remember that POE.pm includes POE::Kernel and POE::Session, so those two modules must be added wherever use POE; is found.

This is equivalent to use POE qw(A B C);

  use POE::Kernel;  # Because POE.pm includes it.
  use POE::Session; # Because POE.pm includes it.
  use POE::A;
  use POE::B;
  use POE::C;

Next a set of preprocessed sources must be created. PerlApp does not support source filters such as POE::Preprocessor, so it is necessary to pre-process modules before they are compiled. This can be done by setting the POE_PREPROC_DUMP environment variable.

  set POE_PREPROC_DUMP=c:\rocco\preproc

Run the program. As the program is run, its preprocessed files will be placed in subdirectories under POE_PREPROC_DUMP.

  perl MyPoeApp.perl

The preprocessed files must now be found, and their parent directory must be placed in the PERL5LIB environment variable.

  C:\rocco\POE-0.20>dir /b /a:d /s c:\rocco\preproc

Shows these directories:

  C:\rocco\preproc\POE
  C:\rocco\preproc\POE\Kernel

Here the POE modules have been dumped into "C:\rocco\preproc\POE" and its subdirectories. In order for use POE::Foo to find POE/Foo.pm, the PERL5LIB environment variable must be set to "c:\rocco\preproc".

  set PERL5LIB=c:\rocco\preproc

It often will not be this trivial to find the value for PERL5LIB, but it will not be much harder.

PerlApp can finally build an EXE version of the program.

  perlapp --exe MyPoeApp.exe --clean MyPoeApp.perl

MyPoeApp.exe should now be a stand-alone executable version of your Perl program.

BUGS

Source filters are line-based, and so is the macro language. The only constructs that may span lines are macro definitions, and those *must* span lines.

The regular expressions that detect and replace code are simplistic and may not do the right things when given challenging Perl syntax to parse. For example, constants are replaced within strings.

Substitution is done in two phases: macros first, then constants. It would be nicer (and more dangerous) if the phases looped around and around until no more substitutions occurred.

The regexp builder makes silly subexpressions like /(?:|m)/. That could be done better as /m?/ or /(?:jklm)?/ if the literal is longer than a single character.

The "# include" directives are not compatible with the POE_PREPROC_DUMP environment variable.

SEE ALSO

The regexp optimizer is based on code in Ilya Zakharevich's Text::Trie.

AUTHOR & COPYRIGHT

POE::Preprocessor is Copyright 2000 Rocco Caputo. Some parts are Copyright 2001 Matt Cashner. All rights reserved. POE::Preprocessor is free software; you may redistribute it and/or modify it under the same terms as Perl itself.