I have a Perl script which invokes other programs, i.e. it calls system and/or exec and/or open with a pipe and/or uses the backtick operator.
Can I run this script in such a way that it will print out the arguments to each of the above so that I can see what it is calling into?
For example a program like this which I cannot modify
#!/usr/bin/perl
sub get_arg {return "argument$_[0]";}
system "./foo", get_arg(1), get_arg(2);
print `./foo abc def`;
Invoked something perhaps like this
perl –shell-trace-on ./myscript.pl
Which will in this case output
./foo argument1 argument2
./foo abc def
It is acceptable to throw away myscript.pl’s normal output or blend it with this trace.
Thanks a lot.
This is considered advanced Perl, but you can define subroutines in the
CORE::GLOBALnamespace at compile time and hijack Perl’s builtin functions.Calling functions in the
COREnamespace will invoke the original builtin functions.To apply this feature to an arbitrary standalone script, you can put this code into a simple module (say,
ShellTrace.pm), and then invokeperlwith the-MShellTraceswitch. (HT: perlman):