How can I translate a shell script to Perl?

前端 未结 6 2028
长情又很酷
长情又很酷 2021-01-30 15:31

I have a shell script, pretty big one. Now my boss says I must rewrite it in Perl. Is there any way to write a Perl script and use the existing shell code as is in my Perl scrip

6条回答
  •  你的背包
    2021-01-30 15:46

    I'm surprised no-one has yet mentioned the Shell module that is included with core Perl, which lets you execute external commands using function-call syntax. For example (adapted from the synopsis):

    use Shell qw(cat ps cp);
    $passwd = cat '

    Provided you use parens, you can even call other programs in the $PATH that you didn't mention on the use line, e.g.:

    gcc('-o', 'foo', 'foo.c');
    

    Note that Shell gathers up the subprocess's STDOUT and returns it as a string or array. This simplifies scripting, but it is not the most efficient way to go and may cause trouble if you rely on a command's output being unbuffered.

    The module docs mention some shortcomings, such as that shell internal commands (e.g. cd) cannot be called using the same syntax. In fact they recommend that the module not be used for production systems! But it could certainly be a helpful crutch to lean on until you get your code ported across to "proper" Perl.

提交回复
热议问题