# perldoc > Sub::Exporter::Tutorial

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

## Quick Reference

- `use Sub::Exporter -setup => { exports => [qw(plus)] }` — basic export setup
- `use Food qw(:fauna)` or `use Food qw(-fauna)` — import a group
- `use Food lox => { -as => 'salmon' }` — rename an import
- `use Food -fauna => { -prefix => 'cute_little_' }` — prefix all imports in a group
- `use Food -fauna => { -suffix => '_meal' }` — suffix all imports in a group
- `use Package::Counter counter => { start => 10 }` — pass arguments to a generator
- `use Menu::Airline allergies => [qw(peanuts)], ethics => [qw(vegan)]` — use collectors
- `use Sub::Exporter -setup => { exports => [ counter => \&gen ] }` — generator as coderef or method name

## Name

**Sub::Exporter::Tutorial** — a friendly guide to exporting with Sub::Exporter

## Synopsis

perl
package MyModule;
use Sub::Exporter -setup => {
  exports => [ qw(func1 func2) ],
  groups  => { group1 => [ qw(func1) ] },
  collectors => [ qw(option) ],
};
Consumers use:
perl
use MyModule qw(func1);                     # import single
use MyModule qw(:group1);                   # import group
use MyModule func1 => { -as => 'new_name' }; # rename
use MyModule -group1 => { -prefix => 'p_' }; # prefix
## Options

Configuration keys passed to `setup_exporter`:

- `exports` — arrayref of export names, or hashref mapping names to generators
- `groups` — hashref mapping group names to arrayrefs of exports (or other groups with renaming)
- `collectors` — arrayref of names that collect import arguments for generators
- `generators` — a coderef or method name that returns a subroutine; can be placed in exports definition

Import-time arguments (per-export or per-group):

- `-as` — rename the imported subroutine; if a scalar ref, stores the coderef
- `-prefix` — prepend a string to all imported names in the group
- `-suffix` — append a string to all imported names in the group
- `-as` with a scalar ref — assign the coderef to the scalar

## Examples

**Basic export setup:**
perl
package Addition;
use Sub::Exporter -setup => { exports => [ qw(plus) ] };
sub plus { my ($x, $y) = @_; return $x + $y; }
**Using export groups:**
perl
package Food;
use Sub::Exporter -setup => {
  exports => [ qw(apple banana beef fluff lox rabbit) ],
  groups  => {
    fauna  => [ qw(beef lox rabbit) ],
    flora  => [ qw(apple banana) ],
  }
};
Consumer: `use Food qw(:fauna);`

**Renaming imports:**
perl
use Food lox => { -as => 'salmon' };
use Food -fauna => { -prefix => 'cute_little_' };
**Generating subroutines (closures):**
perl
package Package::Counter;
sub _build_counter {
  my ($class, $name, $arg) = @_;
  $arg ||= {};
  my $i = $arg->{start} || 0;
  return sub { $i++ };
}
use Sub::Exporter -setup => {
  exports => [ counter => \'_build_counter' ],
  groups  => { default => [ qw(counter) ] },
};
Consumer: `use Package::Counter counter => { start => 10 };`

**Argument collectors:**
perl
package Menu::Airline;
use Sub::Exporter -setup => {
  exports =>  ... ,
  groups  =>  ... ,
  collectors => [ qw(allergies ethics) ],
};
Consumer: `use Menu::Airline allergies => [ qw(peanuts) ], ethics => [ qw(vegan) ];`

**Generating many routines in one scope:**
perl
# Group generator returns a hashref of names => coderefs
use Sub::Exporter -setup => {
  groups => {
    my_group => sub { my ($class, $name, $arg, $collected) = @_; ... return { foo => sub {...}, bar => sub {...} } }
  }
};
## See Also

- [Sub::Exporter](http://localhost/phpMan.php/perldoc/Sub%3A%3AExporter/markdown) — full documentation and references to other exporters