macro - An implementation of macro processor
This document describes macro version 0.06.
use macro add => sub{ $_[0] + $_[1] }; say => sub{ print @_, "\n"}; say(add(1, 3)); # it's replaced into 'print do{ (1) + (3) }, "\n";' use macro my_if => sub{ $_[0] ? $_[1] : $_[2] }; my_if( 0, say('true'), say('false') ); # only 'false' is printed sub mul{ $_[0] * $_[1] } use macro mul => \&mul; say( mul(2, 3) ); # macro version of mul() say(&mul(2, 3) ); # subroutine version say( mul 2, 3 ); # subroutine version # or compile only $ perl -c Module.pm # make Module.pmc
The macro pragma provides macros, a sort of inline functions, which is like C pre-processor's macro.
macro
The macros are very fast (about 200% faster than subroutines), but they have some limitations that C pre-processor's macros have, e.g. they cannot call return() expectedly, although they seem anonymous subroutines.
return()
Try PERL_MACRO_DEBUG=2 if you want to know how this module works.
PERL_MACRO_DEBUG=2
Modules using macro are able to compile themselves before installed, by using the Module::Install::PMC. Write the following to the Makefile.PL and the modules will be compiled at build time.
Module::Install::PMC
Makefile.PL
use inc::Module::Install; ... build_requires macro => 0; pmc_support; ...
See Module::Compile and Module::Install::PMC for details.
Returns the backend module, macro::filter or macro::compiler.
macro::filter
macro::compiler
Returns an instance of macro processor, $macro.
$macro
new(), defmacro() and process() are provided for backend modules.
new()
defmacro()
process()
Defines macros into $macro.
Processes Perl source code $source, and returns processed source code.
Sets the debug mode.
if it's == 0, macro::compiler is used as the backend.
if it's >= 1, macro::filter is used as the backend.
If it's >= 2, all macro expansions are reported to STDERR.
STDERR
To install this module, run the following commands:
perl Makefile.PL make make test make install
Perl 5.8.1 or later.
PPI - Perl parser.
PPI
Filter::Util::Call - Source filter utility (CORE).
Filter::Util::Call
No bugs have been reported.
Please report any bugs or feature requests to bug-macro@rt.cpan.org/, or through the web interface at http://rt.cpan.org/.
bug-macro@rt.cpan.org/
macro::JA.
macro::filter - macro.pm source filter backend.
macro::compiler - macro.pm compiler backend.
Module::Compile.
Goro Fuji <gfuji(at)cpan.org>.
Copyright (c) 2008-2009, Goro Fuji <gfuji(at)cpan.org>. Some rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install macro, copy and paste the appropriate command in to your terminal.
cpanm
cpanm macro
CPAN shell
perl -MCPAN -e shell install macro
For more information on module installation, please visit the detailed CPAN module installation guide.