1Sub::Exporter::Lexical(U3s)er Contributed Perl DocumentatSiuobn::Exporter::Lexical(3)
2
3
4
6 Sub::Exporter::Lexical - to export lexically-available subs with
7 Sub::Exporter
8
10 version 0.092292
11
13 Achtung! I don't know why I wrote this. I don't use it and never
14 have. Originally, it was not lexical, but dynamic, despite the name.
15 What was I thinking? Clearly this was a bad brain day. I have
16 rewritten the code now to use Lexical::Sub, which should make the
17 behavior actually lexical, but I have not expanded the test suite. To
18 continue...
19
20 In an exporting library:
21
22 package Some::Toolkit;
23
24 use Sub::Exporter -setup => {
25 exports => [ qw(foo bar baz) ],
26 };
27
28 sub foo { ... }
29 sub bar { ... }
30 sub baz { ... }
31
32 In an importing library:
33
34 package Vehicle::Autobot;
35
36 use Sub::Exporter::Lexical lexical_installer => { -as => 'lex' };
37
38 ...;
39
40 {
41 use Some:::Toolkit { installer => lex }, qw(foo bar);
42
43 foo(1,2,3);
44 my $x = bar;
45
46 ...
47 };
48
49 # ... and here, foo and bar are no longer available ...
50
52 Sub::Exporter::Lexical provides an alternate installer for
53 Sub::Exporter. Installers are documented in Sub::Exporter's
54 documentation; all you need to know is that by using
55 Sub::Exporter::Lexical's installer, you can import routines into a
56 lexical scope that will be cleaned up when that scope ends.
57
58 There are two places it makes sense to use the lexical installer: when
59 configuring Sub::Exporter in your exporting package or when importing
60 from a package that uses Sub::Exporter. For the first case, do
61 something like this:
62
63 package Some::Toolkit;
64 use Sub::Exporter::Lexical ();
65 use Sub::Exporter -setup => {
66 exports => [ ... ],
67 installer => Sub::Exporter::Lexical::lexical_installer,
68 };
69
70 For the second:
71
72 package My::Library;
73
74 use Sub::Exporter::Lexical ();
75 use Some::Toolkit
76 { installer => Sub::Exporter::Lexical::lexical_installer },
77 qw(foo bar baz);
78
80 Sub::Exporter::Lexical offers only one routine for export, and it may
81 also be called by its full package name:
82
83 lexical_installer
84 This routine returns an installer suitable for use as the "installer"
85 argument to Sub::Exporter. It installs all requested routines as
86 usual, but marks them to be removed from the target package as soon as
87 the block in which it was called is complete.
88
89 It does not affect the behavior of routines exported into scalar
90 references.
91
92 More importantly, it does not affect scopes in which it is invoked at
93 runtime, rather than compile time. This is important! It means that
94 this works:
95
96 {
97 use Some::Toolkit { installer => lexical_installer }, qw(foo);
98 foo(1,2,3);
99 }
100
101 foo(); # this dies
102
103 ...but this does not...
104
105 {
106 require Some::Toolkit;
107 Some::Toolkit->import({ installer => lexical_installer }, qw(foo));
108 foo(1,2,3);
109 }
110
111 foo(); # this does not die, even though you might expect it to
112
113 Finally, you can't supply a "-as => \$var" install destination yet.
114
116 Ricardo Signes <rjbs@cpan.org>
117
119 This software is copyright (c) 2013 by Ricardo Signes.
120
121 This is free software; you can redistribute it and/or modify it under
122 the same terms as the Perl 5 programming language system itself.
123
124
125
126perl v5.32.0 2020-07-28 Sub::Exporter::Lexical(3)