perl - identify a procedure and replace it with a different procedure -
what want achieve:
###############code######## old_procedure(arg1, arg2); #############code_end###### i have huge code has old procedure in it. want call old_procedure go call new procedure (new_procedure(arg1, arg2)) same arguments. know, question seems pretty stupid trick not allowed change code or bad_function. thing can create procedure externally reads code flow or , whenever finds bad_function, replaces new_function. have void type, don't have worry return values. usng perl. if knows how atleast start in direction...please comment or answer. nice if new code can done in perl or c, other known languages too. c++, java.
edit: code written in shell script , perl. cannot edit code , don't have location of old_function, mean can find it...but tough. can use package thing pointed out if there way around it...so parse thread function , replace function calls. please don't remove tags need suggestions java, c++ experts also.
edit: @mirod tried out , answer made new subroutine , there no way of accessing old one. had created variable checks value decide way go( old_sub or new_sub)...is there way add variable in new code...which sends control old_function if not set... like:
use badpackage; # sub defined there begin { package bappackage; no warnings; # avoid "subroutine bad_sub redefined" message # check variable , send old_sub if var not set sub bad_sub { # code } } # @mirod
this easier in perl in lot of other languages, doesn't mean it's easy, , don't know if it's want hear. here's proof-of-concept:
let's take broken code:
# file name: some/package.pm package some::package; use base 'exporter'; our @export = qw(forty_two nineteen); sub forty_two { 19 } sub nineteen { 19 } 1; # file name: main.pl use some::package; print "forty-two plus nineteen ", forty_two() + nineteen(); running program perl main.pl produces output:
forty-two plus nineteen 38 it given files some/package.pm , main.pl broken , immutable. how can fix behavior?
one way can insert arbitrary code perl command -m command-line switch. let's make repair module:
# file: myrepairs.pm check { no warnings 'redefine'; *forty_two = *some::package::forty_two = sub { 42 }; }; 1; now running program perl -mmyrepairs main.pl produces:
forty-two plus nineteen 61 our repair module uses check block execute code in between compile-time , run-time phase. want our code last code run @ compile-time overwrite functions have been loaded. -m command-line switch run our code first, check block delays execution of our repairs until other compile time code run. see perlmod more details.
this solution fragile. can't modules loaded @ run-time (with require ... or eval "use ..." (these common) or subroutines defined in other check blocks (these rare).
if assume shell script runs main.pl immutable (i.e., we're not allowed change perl main.pl perl -mmyrepairs main.pl), move 1 level , pass -mmyrepairs in perl5opt environment variable:
perl5opt="-i/path/to/myrepairs -mmyrepairs" bash the_immutable_script_that_calls_main_pl.sh
Comments
Post a Comment