# perldoc > Sub::Exporter

---
type: CommandReference
command: Sub::Exporter
mode: perldoc
section: ""
source: perldoc
---

## Quick Reference

- `use Sub::Exporter -setup => { exports => [ ... ] }` — configure an exporter in your module
- `use YourModule qw(foo bar baz)` — import routines (same as Exporter)
- `use YourModule foo => { -as => 'bar', option => value }` — import with custom arguments and rename
- `use YourModule -group => { -prefix => 'my_' }` — import a group with prefix
- `use YourModule name => { -as => \my $ref }` — import into a scalar reference (no install)
- `use YourModule collector => { ... }` — pass collections for generators
- `use YourModule { into => 'Target::Package' }, -setup => { ... }` — configure exporter for another package

## Name

Sub::Exporter — a sophisticated exporter for custom-built routines

## Synopsis

perl
# In the exporting module:
package Text::Tweaker;
use Sub::Exporter -setup => {
  exports => [
    qw(squish titlecase),
    reformat => \&build_reformatter,
    trim     => \&build_trimmer,
    indent   => \&build_indenter,
  ],
  collectors => [ 'defaults' ],
};

# In the importing module:
use Text::Tweaker
  'squish',
  indent   => { margin => 5 },
  reformat => { width => 79, justify => 'full', -as => 'prettify_text' },
  defaults => { eol => 'CRLF' };
## Options

### Exporter Configuration (passed to `-setup`)

- `exports` — arrayref or hashref of routine names; each may be followed by a generator (coderef or method name)
- `groups` — arrayref or hashref; values are either a list of exports (with optional arguments) or a generator coderef that returns a hash of name => sub
- `collectors` — list of names; values are passed to every generator. May include validators (coderefs)
- `into_level` — how far up the caller stack to look for a target (default 0)
- `into` — explicit target package for exporting
- `generator` — callback to produce code (default: `Sub::Exporter::default_generator`)
- `installer` — callback to install code (default: `Sub::Exporter::default_installer`)

### Special Import Arguments (per export or group)

- `-as` — rename the exported sub; if a scalar reference, the sub is stored in the ref instead of installed
- `-prefix` — string to prepend to all exports from a group
- `-suffix` — string to append to all exports from a group

### Special Exporter Arguments (passed as first hashref to `use`)

- `into_level`, `into`, `generator`, `installer` — override the same-named configuration options

## Examples

**Basic export with generator:**

perl
package Data::Analyze;
use Sub::Exporter -setup => {
  exports => [ analyze => \&build_analyzer ],
};

sub build_analyzer {
  my ($class, $name, $arg) = @_;
  return sub {
    my $data      = shift;
    my $tolerance = shift || $arg->{tolerance};
    my $passes    = shift || $arg->{passes};
    analyze($data, $tolerance, $passes);
  };
}
**Import with custom defaults and renaming:**

perl
use Data::Analyze
  analyze => { tolerance => 0.10, passes => 10, -as => 'analyze10' },
  analyze => { tolerance => 0.15, passes => 50, -as => 'analyze50' };
**Avoiding name collisions:**

perl
use Morality qw(virtue), sin => { -as => 'offense' };
use Math::Trig -all => { -prefix => 'trig_' };
**Groups with generators:**

perl
package Data::Crypto;
use Sub::Exporter -setup => {
  groups => { cipher => \&build_cipher_group },
};

sub build_cipher_group {
  my ($class, $group, $arg) = @_;
  my ($encode, $decode) = build_codec($arg->{secret});
  return { cipher => $encode, decipher => $decode };
}
**Using collectors for global defaults:**

perl
use Data::Analyze
  'analyze',
  analyze  => { tolerance => 0.10, -as => 'analyze10' },
  analyze  => { tolerance => 0.15, passes => 50, -as => 'analyze50' },
  defaults => { passes => 10 };
**Installing into a different package:**

perl
use Sub::Exporter
  { into => 'Target::Package' },
  -setup => { exports => [ ... ] };
## See Also

- [Sub::Exporter::Tutorial](https://metacpan.org/pod/Sub::Exporter::Tutorial)
- [Exporter](https://metacpan.org/pod/Exporter)
- [Exporter::Lite](https://metacpan.org/pod/Exporter::Lite)
- [Exporter::Easy](https://metacpan.org/pod/Exporter::Easy)
- [Exporter::Simple](https://metacpan.org/pod/Exporter::Simple)
- [Exporter::Renaming](https://metacpan.org/pod/Exporter::Renaming)
- [Class::Exporter](https://metacpan.org/pod/Class::Exporter)
- [Exporter::Tidy](https://metacpan.org/pod/Exporter::Tidy)
- [Perl6::Export](https://metacpan.org/pod/Perl6::Export)
- [Perl6::Export::Attrs](https://metacpan.org/pod/Perl6::Export::Attrs)