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

Respected Monks,

I recently inherited some legacy perl-code (in a Linux environment) that to a large extent contains shell script code such as:
$dir = "/tmp/foo"; `rm -rf $dir`;
I often find myself re-writing these statements using built-in perl functions or CPAN modules: I dislike the 'shell' technique for a number of reasons, namely because What is the Monks opinion on mixing shell-script in perl code? The subject has previously been discussed here. Should I use perl if it's possible and easy? Which are the obvious and not-so-obvious candidates for re-implementation?

Best regards,
--
Andreas

Replies are listed 'Best First'.
Re: Re-write shell in perl
by jesuashok (Curate) on May 03, 2007 at 08:43 UTC
Re:Re-write shell in perl
by Moron (Curate) on May 03, 2007 at 14:01 UTC
    I'd say that objection 2 is more obviously resolved using either IPC::Open3 or IPC::Run3. The other two arguments remain, unless of course there is no Perl equivalent, so e.g. the unix command
    comm [-123] file1 file2
    if used repeatedly should be replaced with a handrolled Perl equivalent to prevent process creation overheads, or else provided your expectations are Unix only and non-repeating you could still allow the odd shelling out with IPC.
    __________________________________________________________________________________

    ^M Free your mind!

Re: Re-write shell in perl
by swares (Monk) on May 03, 2007 at 15:57 UTC
    Plus, there is the forking required... and the resources and time that takes. I have seen real speed improvements by using Perl rather than shell commands. Shell scripts do not have the flexibility of running Perl. Little things trip them up. Perl is much more robust. Most of the time I get to writing something in shell I later realize some part of it would be easy or if not easy at least possible, if only I was using Perl.