1Sub::Install(3) User Contributed Perl Documentation Sub::Install(3)
2
3
4
6 Sub::Install - install subroutines into packages easily
7
9 version 0.928
10
12 use Sub::Install;
13
14 Sub::Install::install_sub({
15 code => sub { ... },
16 into => $package,
17 as => $subname
18 });
19
21 This module makes it easy to install subroutines into packages without
22 the unsightly mess of "no strict" or typeglobs lying about where just
23 anyone can see them.
24
26 install_sub
27 Sub::Install::install_sub({
28 code => \&subroutine,
29 into => "Finance::Shady",
30 as => 'launder',
31 });
32
33 This routine installs a given code reference into a package as a normal
34 subroutine. The above is equivalent to:
35
36 no strict 'refs';
37 *{"Finance::Shady" . '::' . "launder"} = \&subroutine;
38
39 If "into" is not given, the sub is installed into the calling package.
40
41 If "code" is not a code reference, it is looked for as an existing sub
42 in the package named in the "from" parameter. If "from" is not given,
43 it will look in the calling package.
44
45 If "as" is not given, and if "code" is a name, "as" will default to
46 "code". If "as" is not given, but if "code" is a code ref,
47 Sub::Install will try to find the name of the given code ref and use
48 that as "as".
49
50 That means that this code:
51
52 Sub::Install::install_sub({
53 code => 'twitch',
54 from => 'Person::InPain',
55 into => 'Person::Teenager',
56 as => 'dance',
57 });
58
59 is the same as:
60
61 package Person::Teenager;
62
63 Sub::Install::install_sub({
64 code => Person::InPain->can('twitch'),
65 as => 'dance',
66 });
67
68 reinstall_sub
69 This routine behaves exactly like "install_sub", but does not emit a
70 warning if warnings are on and the destination is already defined.
71
72 install_installers
73 This routine is provided to allow Sub::Install compatibility with
74 Sub::Installer. It installs "install_sub" and "reinstall_sub" methods
75 into the package named by its argument.
76
77 Sub::Install::install_installers('Code::Builder'); # just for us, please
78 Code::Builder->install_sub({ name => $code_ref });
79
80 Sub::Install::install_installers('UNIVERSAL'); # feeling lucky, punk?
81 Anything::At::All->install_sub({ name => $code_ref });
82
83 The installed installers are similar, but not identical, to those
84 provided by Sub::Installer. They accept a single hash as an argument.
85 The key/value pairs are used as the "as" and "code" parameters to the
86 "install_sub" routine detailed above. The package name on which the
87 method is called is used as the "into" parameter.
88
89 Unlike Sub::Installer's "install_sub" will not eval strings into code,
90 but will look for named code in the calling package.
91
93 Sub::Install exports "install_sub" and "reinstall_sub" only if they are
94 requested.
95
96 exporter
97 Sub::Install has a never-exported subroutine called "exporter", which
98 is used to implement its "import" routine. It takes a hashref of named
99 arguments, only one of which is currently recognize: "exports". This
100 must be an arrayref of subroutines to offer for export.
101
102 This routine is mainly for Sub::Install's own consumption. Instead,
103 consider Sub::Exporter.
104
106 Sub::Installer
107 This module is (obviously) a reaction to Damian Conway's
108 Sub::Installer, which does the same thing, but does it by getting
109 its greasy fingers all over UNIVERSAL. I was really happy about
110 the idea of making the installation of coderefs less ugly, but I
111 couldn't bring myself to replace the ugliness of typeglobs and
112 loosened strictures with the ugliness of UNIVERSAL methods.
113
114 Sub::Exporter
115 This is a complete Exporter.pm replacement, built atop
116 Sub::Install.
117
119 Several of the tests are adapted from tests that shipped with Damian
120 Conway's Sub-Installer distribution.
121
123 Ricardo SIGNES <rjbs@cpan.org>
124
126 This software is copyright (c) 2005 by Ricardo SIGNES.
127
128 This is free software; you can redistribute it and/or modify it under
129 the same terms as the Perl 5 programming language system itself.
130
131
132
133perl v5.32.0 2020-07-28 Sub::Install(3)