Exporter::Lexical - exporter for lexical subs


version 0.02


  package My::Exporter;
  use Exporter::Lexical -exports => [ 'foo' ]
  sub foo { "FOO" }

  package MyApp;

      use My::Exporter;
      warn foo(); # FOO
  warn foo(); # Undefined subroutine &main::foo called


This module allows you to export lexical subs from your exporter module. It is implemented using the new lexical_subs feature in perl 5.18, so the functions truly are lexical (unlike some of the previous attempts).

This module is quite experimental, and may change a lot in the future as I figure out how it should work. It is very much a proof of concept for the moment.

This module takes a hash of import args as configuration, with these keys:


The value of this key can either be an arrayref, in which case the elements of the arrayref will be treated as function names to look for in the current package and export, or a hashref, in which case the keys will be function names and the values will be coderefs.

Importing this module will also install an import method into your module, which will handle the exporting process as configured by the import args.


build_exporter(\%opts[, $caller])

  my $import Exporter::Lexical::build_exporter({
      -exports => ['foo'],

This function just creates the method that it would install as your package's import method, without actually installing it. This lets you write your own import method that does whatever you want it to do, while still being able to export from it.

This function takes a hashref of arguments which correspond to the arguments you would pass to import, followed by an optional package name to get the subs from, if you're exporting subs by name. The default is to get the subs from the calling package.

lexical_import($name, $sub)

Installs $sub as a lexical subroutine into the currently compiling lexical scope. Throws an error if there is no currently compiling lexical scope (for instance, if this is called at runtime).


No known bugs.

Please report any bugs through RT: email bug-exporter-lexical at, or browse to




"The 'lexical_subs' feature" in feature


You can find this documentation for this module with the perldoc command.

    perldoc Exporter::Lexical

You can also look for information at:


Jesse Luehrs <>


This software is Copyright (c) 2013 by Jesse Luehrs.

This is free software, licensed under:

  The MIT (X11) License