Perl: when do you use system() and when do you install the package?

旧街凉风 提交于 2019-12-08 00:15:28

问题


I work in a project that uses Perl scripts on machines that are maintained by system folks, and installing packages such as Perl modules is no easy task - you usually have to call the person with the permissions to do that, wait several days, learn the API of the package, and then remember to install it on every newly configured machine ever installed.

The alternative that is many times chosen is just calling system() (or backtick notation, ``, inside Perl) and using the output of the shell command that does what you want. Of course it's not always possible, and when it use, there is usually a wrapping to do around the command call, but it's generally easier.

My question is when is the trade-off pulling towards either side, in your experience?

Edit: adding an example: I want to print the total size of a directory in a human-readable format, or list all regular files bigger than a certain size, and du seems to make this easier than installing modules that do the same thing...


回答1:


The core issue seems to be the perceived difficulty and length of time to install Perl modules. I would identify why they have problems installing the packages and try and help streamline their process.

A common solution is to modify your process. Admins don't typically like to install straight from CPAN, but as a developer you can use a local CPAN repo. You "freeze" the packages you use there, and then promote them for use in production.

That said the trade-off between using modules or shelling it out as follows:

Data

Modules typically return structured data, shelling out returns unstructured text that you have to parse.

Performance/Resource Usage

Shelling out creates a whole other process, modules usually provide functionality within the current operating process.

Additional Dependencies

Shelling out makes your program dependent on whatever you're shelling out to. Keep in mind that some basic programs change in output and behavior from OS to OS, so you may also be coupling yourself to a particular OS or set of OSes.




回答2:


Oh, that's an easy one. One always prefers modules because the tighter integration makes it possible to pass around data that do not suit the traditional IPC.


What, did you expect help in rationalising your suffering under crappy sysadminship?




回答3:


Modules, always modules. And, when I say always, I mean "almost always."

Modules are easier to control and to debug. It's easier to require a module be installed than some unrelated utility. It's more portable to use a module than to rely on the chancy command line syntax of a particular command.




回答4:


You have a bunch of modules that go with the core distro. Just use them.

You can install modules right in your home directory and when the time comes negotiate with the sysadmins: http://perl.jonallen.info/writing/articles/install-perl-modules-without-root




回答5:


As said above, always modules, because ls ain't dir...



来源:https://stackoverflow.com/questions/4545074/perl-when-do-you-use-system-and-when-do-you-install-the-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!