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,
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.