Introducing Begin::Declare

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, without having to write the variable names twice. 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.

Here is the synopsis from the module:

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

The module lifts the computation of the rhs to compile time, as well as the assignment.


I'm sorry for playing the ignorant here, but... why is this important? Why is it crucial to have the value 'fooval' in the scalar $foo in compile-time?

I see this mostly useful for modules that can be configured with import() parameters, like Exception::Class or Sub::Exporter. A small contrived example would be this piece of code, showing both the old and the new way:

Ah I see.

Thank you for explaining. :)

Very nice. Removing a pessimism, small though it may seem. :-)

Leave a comment

About Eric Strom (ASG)