Export::Lexical

cgrau on 2008-10-09T18:01:29

I'm currently on a brief leave from $work after the birth of my daughter last Tuesday. Since she hasn't required that much attention, I wanted to take advantage of the time off and be productive. I'm always talking about how I want to write more code to release, so I've finally put my money where my mouth is. I released Export::Lexical last night.

It came from an aside made during a Perl 5.10 class taught by Damian Conway last week. He was demonstrating the lexically scoped module feature (see perlpragma) with a simple debugging module with lexical scope. When asked if it was available on the CPAN, he said it wasn't, because he would rather see a module available that generalized the technique. When he suggested that someone in the audience might be the one to write it, I jumped.

It's a fairly simple and straight-forward module, providing a single subroutine attribute: :ExportLexical. When marked with this attribute, the subroutine is then exported only to the lexical scope in which the module is used. The no keyword also works, in the same way as no strict and no warnings. Additionally, individual subroutines can be specified in the same way as use strict 'refs' or no warnings 'redefine' are used.

The module makes all of this easy, without the need to fully understand perlpragma or the need to code up all the boilerplate infrastructure. We like easy, right?

package Foo;

use strict;
use warnings;
use Export::Lexical;

sub foo :ExportLexical {
    # do something
}

sub bar :ExportLexical {
    # do something else
}

1;

No, really. That's it. It's rather simple to use the resulting module as well.

#!perl

use strict;
use warnings;

{
    use Foo;

    foo();    # calls foo()
    bar();    # calls bar()

    {
        no Foo 'bar';

        foo();    # calls foo()
        bar();    # bar() is a no-op
    }
}

I'm pretty excited that I've finally taken the time to contribute. Hopefully this will get me on a roll and will be the first of many. I actually have several I've developed at $work to simplify programs written to work with LSF, but I'm still waiting for legal to allow me to publish them under an Open Source license.


cool!

rjbs on 2008-10-09T19:45:18

I'd much prefer to be able to make "bar" after "no X 'bar'" be an exception rather than a no-op.

I'd also love to see a Sub::Exporter extension (this could be a kind of 'installer').

Re:cool!

cgrau on 2008-10-09T20:39:37

I'd much prefer to be able to make "bar" after "no X 'bar'" be an exception rather than a no-op.

That had crossed my mind as well. In this initial release, I went ahead and let the module quietly ignore the call. I intend to make exceptions an option (or the default behavior - I'm not sure yet).

I'd also love to see a Sub::Exporter extension (this could be a kind of 'installer').

I'll look at that. Thanks for the suggestion.

will not work

chorny on 2009-08-19T07:53:30

Will not work due to the same problem with sub attributes as in Exporter::Simple.
Reported it here: https://rt.cpan.org/Ticket/Display.html?id=48824