smg1827 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I got a Perl script from my colleague which I am going to run on my server.

This script is written to setup some folder structure and to create some users/group/permission/ownership. So, most part of the script uses backtick to call OS commands with some logic behind it. (my question is not about whether backtick is good or bad).

I want to make a minimum changes to the script so that every backtick with variable interpolation (example: `cd $v` should be recorded as 'cd /var/tmp', same as executed by OS) is recorded on STDOUT/STDERR or to some log file.

I don't want to go and add print statement just before every backticks.

What should be the good minimum change (either internal change or thru some wrapper) with or without any modules would help my scenario?

I am thinking of *analogy* like $SIG{__DIE__} = sub {}

I am looking for a summary of chmod, chown, mkdir OS commands executed by this perl script.

Thanks.

  • Comment on record every backtick on STDOUT or some log file

Replies are listed 'Best First'.
Re: record every backtick on STDOUT or some log file
by kennethk (Abbot) on Apr 23, 2012 at 21:05 UTC

    Unfortunately, I think the simplest way to make this happen is to bite the bullet and mod the code. Assuming the commands are not too full of special characters, you should be able to get away with running a backup of the file through s/`([^`]*)`.*?;\K/print \$log qq{$1};/g; to automatically add print statements after all backticks. Obviously, you'll have to open the $log filehandle.

    Alternatively, you could hack a version of perl to output everything passed to backticks...

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: record every backtick on STDOUT or some log file
by Anonymous Monk on Apr 24, 2012 at 06:09 UTC
Re: record every backtick on STDOUT or some log file
by Anonymous Monk on Apr 23, 2012 at 22:18 UTC
    Requires perl>=5.14.0 (/r)
    use strict; use warnings; my $v = '/tmp/test'; while(<DATA>){ s{`([^`\\]*(?>\\.|[^`\\]*)*)`}{$1 =~ s{(\$\w+)}{$1}eegr }eg; print; } __DATA__ test --`cd \` $v`--
Re: record every backtick on STDOUT or some log file
by thargas (Deacon) on Apr 25, 2012 at 11:42 UTC
    Personally, I'd replace all backticks with a call to a function/method with the string as an arg. Then you can do whatever you want there. And change your mind later easily.