1Sub::Exporter::ForMethoUdsse(r3)Contributed Perl DocumenStuabt:i:oEnxporter::ForMethods(3)
2
3
4
6 Sub::Exporter::ForMethods - helper routines for using Sub::Exporter to
7 build methods
8
10 version 0.100050
11
13 In an exporting library:
14
15 package Method::Builder;
16
17 use Sub::Exporter::ForMethods qw(method_installer);
18
19 use Sub::Exporter -setup => {
20 exports => [ method => \'_method_generator' ],
21 installer => method_installer,
22 };
23
24 sub _method_generator {
25 my ($self, $name, $arg, $col) = @_;
26 return sub { ... };
27 };
28
29 In an importing library:
30
31 package Vehicle::Autobot;
32 use Method::Builder method => { -as => 'transform' };
33
35 The synopsis section, above, looks almost indistinguishable from any
36 other use of Sub::Exporter, apart from the use of "method_installer".
37 It is nearly indistinguishable in behavior, too. The only change is
38 that subroutines exported from Method::Builder into named slots in
39 Vehicle::Autobot will be wrapped in a subroutine called
40 "Vehicle::Autobot::transform". This will insert a named frame into
41 stack traces to aid in debugging.
42
43 More importantly (for the author, anyway), they will not be removed by
44 namespace::autoclean. This makes the following code work:
45
46 package MyLibrary;
47
48 use Math::Trig qw(tan); # uses Exporter.pm
49 use String::Truncate qw(trunc); # uses Sub::Exporter's defaults
50
51 use Sub::Exporter::ForMethods qw(method_installer);
52 use Mixin::Linewise { installer => method_installer }, qw(read_file);
53
54 use namespace::autoclean;
55
56 ...
57
58 1;
59
60 After MyLibrary is compiled, "namespace::autoclean" will remove "tan"
61 and "trunc" as foreign contaminants, but will leave "read_file" in
62 place. It will also remove "method_installer", an added win.
63
65 Sub::Exporter::ForMethods offers only one routine for export, and it
66 may also be called by its full package name:
67
68 method_installer
69 This routine returns an installer suitable for use as the "installer"
70 argument to Sub::Exporter. It updates the "\@to_export" argument to
71 wrap all code that will be installed by name in a named subroutine,
72 then passes control to the default Sub::Exporter installer.
73
75 Ricardo Signes <rjbs@cpan.org>
76
78 This software is copyright (c) 2010 by Ricardo Signes.
79
80 This is free software; you can redistribute it and/or modify it under
81 the same terms as the Perl 5 programming language system itself.
82
83
84
85perl v5.12.3 2010-01-06 Sub::Exporter::ForMethods(3)