Using UNIX command in perl script.

I wish to know if there is any limitation in using unix commands in perl script or it is just we should avoid using them in our perl script.

For e.g Below is the command to get the recent file in a dir.:

$lcsvFile = `cd "$l_inputfilepath";ls -1t *.CSV|tail -1`

Is there any harm in coding as done above ?

No it not harmful per se. But if your perl is nothing but shell commands then your code belongs in a shell script.

Sometimes writing the perl equivalent to a simple shell command is not worth it, unless you want performance. Performance meaning the command is inside a loop that executes 10000 times. Once or twice is perfectly fine and has an almost imperceptible effect on performance. 100's of shell calls - no way....

What you are probably doing is using an efficient shell builtin - ls - and other than creating child processes is way more efficient the opendir(), readir(), etc. in perl - because those system API's are buried inside perl pm's. And perl is interpreted.

1 Like