0

I'm learning Perl. I have a trivial application that consists of a bash script wrapper around a Perl script. I am thinking about getting rid of the Bash script and doing it all in Perl (as a learning exercise).

I have seen at least three different ways of calling system commands from Perl: backticks, exec() and system().

I would like to know which is preferred. I have read:

Both Perl's exec() function and system() function execute a system shell command. The big difference is that system() creates a fork process and waits to see if the command succeeds or fails—returning a value. exec() does not return anything, it simply executes the command. Neither of these commands should be used to capture the output of a system call. If your goal is to capture output, you should use the backtick operator.

In the case of my example code (below), should I mix these types of system calls, or just keep it simple and use backticks?

Second, I would like to know if it is recommended to move the functions of my bash script wrapper into my Perl script. For example, if I were doing something simple like this on the job, could I expect to be criticized for an implementation that consists of two scripts in two different languages, or would I be criticized for doing stuff in Perl that could easily be done in Bash? Or is it "whatever I prefer"?

#!/bin/bash

rm myfile.zip >/dev/null 2>&1
wget https://downloads.example.com/myfile.zip
if [[ $? -ne 0 ]]; then
        echo "ERROR: failed to download list from example.com."
        exit 1
fi

current_date=$(date +"%Y-%m-%d")
read -p "Save current input as input_$current_date.json then press Enter key to continue."
echo

current_datetime=$(date +"%Y-%m-%d_%H%M")
mylist="mylist_$current_datetime.txt"

zipinfo -1 myfile.zip | sed 's|myfile_xyz/||' | grep '^me' | sed 's/.top.myfile//'  > $mylist

perl myperlscript.pl $mylist "input_$current_date.json"

bash is the default shell on my system, so that's what Perl will use unless told otherwise. I'm not concerned about portability to other systems or other shells.

3

I have seen at least three different ways of calling system commands from Perl: backticks, exec() and system().

I would like to know which is preferred. I have read:

Which is preferred depends entirely on what you want to do. The three mechanisms do very different things.

  • Backticks are used when you want to capture the output of a program.
  • exec() is used when you want to replace your script with a new binary. Execution of your perl script stops at the point you call exec() and never continues.
  • system() is used to when you want to run a command and then continue to execute your perl script.

Second, I would like to know if it is recommended to move the functions of my bash script wrapper into my Perl script.

That's generally a matter of personal preference, but for the things you're doing in your script, it would probably simplify things to just move all that logic into your perl code (e.g., perl already has date/time functions that would trivially replace your various calls to date).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.