<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Eric Strom (ASG)</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/eric_strom_asg/" />
    <link rel="self" type="application/atom+xml" href="http://blogs.perl.org/users/eric_strom_asg/atom.xml" />
    <id>tag:blogs.perl.org,2009-11-03:/users/eric_strom_asg//1030</id>
    <updated>2011-12-18T01:55:36Z</updated>
    <subtitle>A blog about the Perl programming language</subtitle>
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type Pro 4.38</generator>

<entry>
    <title>Introducing here</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/eric_strom_asg/2011/12/introducing-here.html" />
    <id>tag:blogs.perl.org,2011:/users/eric_strom_asg//1030.2581</id>

    <published>2011-12-18T01:19:53Z</published>
    <updated>2011-12-18T01:55:36Z</updated>

    <summary>I&apos;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...</summary>
    <author>
        <name>Eric Strom (ASG)</name>
        <uri>https://metacpan.org/author/ASG</uri>
    </author>
    
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/eric_strom_asg/">
        <![CDATA[<p>I've just published the first release of the <tt>here</tt> 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.  </p>

<p>you operate on code as data, as you normally would when metaprogramming.  then a call to <tt>use here $generated_code;</tt> injects that code into the compiling source<br />
<code><br />
sub my_0 {map "my \$$_ = 0", @_}  # a simple macro</p>

<p>use here my_0 qw(x y z);<br />
</code></p>

<p>results in perl compiling <br />
<code><br />
my $x = 0; my $y = 0; my $z = 0;<br />
</code></p>

<p>and all of those variables are of course in scope after the use here line.</p>

<p><a href="https://metacpan.org/module/here"><tt>here</tt> on metacpan</a></p>

<p>an example module using <tt>here</tt> is <a href="https://metacpan.org/module/here::declare"><tt>here::declare</tt></a>, which provides compile time assignment to lexical variables as well as a shortcut for defining constants.<br />
<code><br />
use here::declare;</p>

<p>use my '@foo' => [1, 2, 3];<br />
</code></p>

<p>results in perl compiling<br />
<code><br />
my @foo; BEGIN {@foo = (1, 2, 3)}<br />
</code></p>

<p>or to define a constant:<br />
<code><br />
use const2 DEBUG => 1;<br />
</code></p>

<p>which results in:<br />
<code><br />
sub DEBUG () {1} our $DEBUG; BEGIN {*DEBUG = \DEBUG}<br />
</code></p>

<p>giving you both the <tt>$DEBUG</tt> constant scalar, and the <tt>DEBUG</tt> constant subroutine with one simple declaration.</p>

<p>feedback welcome.</p>]]>
        
    </content>
</entry>

<entry>
    <title>Introducing Begin::Declare</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/eric_strom_asg/2011/11/introducing-begindeclare.html" />
    <id>tag:blogs.perl.org,2011:/users/eric_strom_asg//1030.2437</id>

    <published>2011-11-13T22:03:34Z</published>
    <updated>2011-11-13T22:52:12Z</updated>

    <summary>While working on a module for source code injection, one of my example &quot;macros&quot; was one that allows you to declare and assign to a lexical variable at compile time, without having to write the variable names twice. That concept...</summary>
    <author>
        <name>Eric Strom (ASG)</name>
        <uri>https://metacpan.org/author/ASG</uri>
    </author>
    
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/eric_strom_asg/">
        <![CDATA[<p>While working on a module for source code injection, one of my example "macros" was one that allows you to declare and assign to a lexical variable at compile time, <em>without having to write the variable names twice</em>.  That concept more or less hijacked the conversation, and after some prodding from p5p to make the interface better, I've used Devel::Declare to add two new keywords to Perl, MY and OUR.</p>

<p>Here is the synopsis from the module:</p>

<blockquote>
don't you hate writing:
<pre>
my ($foo, @bar);
BEGIN {
    ($foo, @bar) = ('fooval', 1 .. 10);
}
</pre>
when you should be able to write:
<pre>
MY ($foo, @bar) = ('fooval', 1 .. 10);
</pre>
just use Begin::Declare; and you can.
</blockquote>

<p>The module lifts the computation of the rhs to compile time, as well as the assignment.</p>

<p><a href="https://metacpan.org/module/Begin::Declare">https://metacpan.org/module/Begin::Declare</a><br />
</p>]]>
        
    </content>
</entry>

</feed>
