Introducing here

I've just published the first release of the here module, a mechanism for safely inserting generated code into a compiling program. this can be used to write macros, cut down on boiler plate, and to implement new declarations, all without ever having to parse any perl source code.

you operate on code as data, as you normally would when metaprogramming. then a call to use here $generated_code; injects that code into the compiling source

sub my_0 {map "my \$$_ = 0", @_} # a simple macro

use here my_0 qw(x y z);

results in perl compiling

my $x = 0; my $y = 0; my $z = 0;

and all of those variables are of course in scope after the use here line.

here on metacpan

an example module using here is here::declare, which provides compile time assignment to lexical variables as well as a shortcut for defining constants.

use here::declare;

use my '@foo' => [1, 2, 3];

results in perl compiling

my @foo; BEGIN {@foo = (1, 2, 3)}

or to define a constant:

use const2 DEBUG => 1;

which results in:

sub DEBUG () {1} our $DEBUG; BEGIN {*DEBUG = \DEBUG}

giving you both the $DEBUG constant scalar, and the DEBUG constant subroutine with one simple declaration.

feedback welcome.

1 Comment

This is just awesome!!!

Leave a comment

About Eric Strom (ASG)

user-pic https://metacpan.org/author/ASG