NAME
Acme::SubstituteSubs - Replace subroutines at run-time
SYNOPSIS
use Acme::SubstituteSubs;
sub say_hi { print "hihihihi!\n"; }
my $say_hi = Acme::SubstituteSubs->get('main::say_hi') or die;
$say_hi =~ s/"hi/"hihi/;
Acme::SubstituteSubs->set('main::say_hi', $say_hi) or die;
say_hi();
exec 'perl', $0;
DESCRIPTION
Replaces subroutine definitions in the source code, probably for code that edits itself or lets its user edit it.
Acme::SubstituteSubs->get($qualified_function_name)
Returns the text of the named function straight from the source file. For the purposes of this module, all code comes from and goes to the top-level .pl
file as indicated by FindBin's $RealScript
value. Returns nothing if the sub is not found.
Acme::SubstituteSubs->set($qualified_function_name, $replacement_code)
Replaces the copy of the function or method specified by $qualified_function_name
with the code specified in $replacement_code
in the source code of the script (see above). set
uses B::Deparse if passed a coderef.
If the function name doesn't already exist, it'll be added to the end of the appropriate package. If the package doesn't already exist in the source file of the script, it'll be added to the end and the new function placed after it.
If attempting to replace a function defined elsewhere than the top level .pl
file, such as in some module, the module won't be changed, but the code will instead be replicated into the main script. The result is undefined when run from perl -e
.
die
s if it fails to write and replace the original source file.
Acme::SubstituteSubs->list()
Lists namespace::function
combinations available for edit.
Acme::SubstituteSubs->list_packages()
Lists packages defined in the source script.
TODO/BUGS
- Needs a REPL plugin, so REPLs can call this when the user redefines a subroutine.
- Parses the document again each time a method is called rather than caching it. Bug.
- There's gotta be a better way to use the PPI API but I just could not get the
replace
method to work. - Should have been called Acme::ModifyMethods?
- Somehow tie or watch the stash and automatically decompile and write out new subroutines on change?
- Hardly tested this at all. I'd wait for 0.02 if I were you.
HISTORY
- 0.02
-
Fixed the example.
- 0.01
-
Original version; created by h2xs 1.23 with options
-A -C -X -b 5.8.0 -c -n Acme::SubstituteSubs --extra-anchovies
SEE ALSO
- PPI
- Acme::State
- Acme::MUD
- Continuity::Monitor
-
If you're using Acme modules, a therapist.
AUTHOR
Scott Walters, <scott@slowass.net>
COPYRIGHT AND LICENSE
Copyright (C) 2009 by Scott Walters
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.9 or, at your option, any later version of Perl 5 you may have available.
4 POD Errors
The following errors were encountered while parsing the POD:
- Around line 65:
'=item' outside of any '=over'
- Around line 77:
You forgot a '=back' before '=head2'
- Around line 98:
'=item' outside of any '=over'
- Around line 108:
You forgot a '=back' before '=head1'