It's still not entirely clear to me what you want to achieve.
In case the idea is to override a Perl builtin (kind of analogously to
what you could use LD_PRELOAD for with C libs), you could use
CORE::GLOBAL, e.g. like this (I'm using getppid() in the
example, as Perl has no builtin named getpid() ):
#!/usr/bin/perl
BEGIN {
*CORE::GLOBAL::getppid = sub () {
return "Hello world!\n" . CORE::getppid();
}
}
print getppid();
(but note that you can only override builtins whose effective prototype
can be expressed as a Perl prototype — print() for example
cannot be overridden due to its print FH $something, ... syntax
(i.e. no comma after FH) )
When you put the override code into a module (say "mygetppid.pm"):
*CORE::GLOBAL::getppid = sub () {
return "Hello world!\n" . CORE::getppid();
};
1;
you could then write
$ perl -Mmygetppid -e "print getppid()"
Hello world!
23726
where the -Mmygetppid is sematically similar to
saying something like LD_PRELOAD=mygetppid.so in order to override some
function call made from a given C binary.
OTOH, if the idea is to use the regular LD_PRELOAD mechanism to override some C library function which is
being called under the hood by perl, you can do it just the same way you would if that library
function was being called by some other binary. For example,
Re: mocking or trapping system calls shows how to override the C lib execvp() call (invoked from perl).
|