Using Perl modules vs. using system() calls
Asked Answered
A

1

5

Quite recently, I wrote a few scripts in Perl for a cPanel plugin in which, though most of the code was in Perl, there was quite a lot of system() commands as well which I used to execute shell commands directly.

I am pretty sure that there are Perl modules that I could have used instead. Keeping in mind the time crunch, I thought using the system command was easier (to complete the project in time). In retrospective, I think that was a bad programming practice.

My question is, is there any tradeoff, memory-wise or otherwise when using Perl's modules and using system() commands. For example, what would be the difference in using:

my $directory = "temp";
mkdir $directory;

and

system ("mkdir temp");

Also, if I am to use Perl modules, wouldn't that involve installing a whole lot of modules in the beginning?

Andreaandreana answered 30/11, 2013 at 10:45 Comment(1)
Many modules are Core, meaning they come with any standard perl installation. And many EXTREMELY useful modules are not Core, and you should take the trouble to install them anyway if you end up needing their functionality. CPAN is fairly easy to use and has a lot to offer.Dissertation
P
10

The most obvious economy is that, in the first case, your Perl process is creating the directory, while in the second, Perl is starting a new process that runs a command shell which parses the command line and runs the shell mkdir command to create the directory, and then the child process is deleted. You would be creating and deleting a process and running the shell for every call to system: there is no caching of processes or similar economy.

The second thing that comes to mind is that, if your original mkdir fails, it is simple to handle the error in Perl, whereas shelling out to run a mkdir command puts your program at a distance from the error, and it is far more awkward to handle the many different problems that may arise.

There is also the question of maintainability and portability, which will affect you even if you aren't expecting to run your program on more than one machine. Once you abandon control to a system command you have no control over what happens. I could have written a mkdir that will delete your home directory or, less disastrously, your program may find itself on a system where mkdir doesn't exist, or does something slightly different.

In the particular case of mkdir, this is a built-in Perl operator and is part of every Perl installation. There are also many core libraries that require you to put use Module in your program, but are already installed and need no further action.

I am sure others will come up with more reasons to prefer a Perl operator or module over a shell command. In general you should prefer to keep everything you can within the language. There are only a few cases where you have to run a third-party program, and they usually involve custom software that allows you act on proprietary data formats.

Principalities answered 30/11, 2013 at 10:56 Comment(1)
Re "it is simple to handle the error in Perl", You also have more flexibility in how you handle them. Don't want anything on STDERR? No problem!Mercedezmerceer

© 2022 - 2024 — McMap. All rights reserved.