Begin::Declare - compile time lexical variables


version 0.06


don't you hate writing:

    my ($foo, @bar);
    BEGIN {
        ($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.


    use Begin::Declare;

is the same as:

    use Begin::Declare qw (MY OUR); # and STATE if "use feature 'state';"

you can also write:

    use Begin::Declare qw (My Our);

if you prefer those names.


MY ... = ...;

    works just like the keyword my except it lifts the assignment to compile time.

        MY $x = 1;            # my $x; BEGIN {$x = 1}
        MY ($y, $z) = (2, 3); # my ($y, $z); BEGIN {($y, $z) = (2, 3)}

OUR ... = ...;

    works just like the keyword our except it lifts the assignment to compile time.

        OUR ($x, @xs) = 1 .. 10;  # our ($x, @xs); BEGIN {($x, @xs) = 1 .. 10}

STATE ... = ...;

    works better than the keyword state (since it supports list assignment and is a proper lvalue (at least on it's rhs)) and it lifts the assignment to compile time.

        STATE ($x, @xs) = 1 .. 10;  # state ($x, @xs); BEGIN {($x, @xs) = 1 .. 10}
        for (1 .. 5) {
            print ++STATE $x = 'a';  # bcdef


Eric Strom, <asg at>


please report any bugs or feature requests to bug-begin-declare at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


the authors of Devel::Declare


copyright 2011 Eric Strom.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

see for more information.