Perl: when do you use system() and when do

2019-05-21 10:38发布

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

5条回答
趁早两清
2楼-- · 2019-05-21 10:46

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

查看更多
我只想做你的唯一
3楼-- · 2019-05-21 11:02

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

查看更多
劫难
4楼-- · 2019-05-21 11:05

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.

查看更多
Bombasti
5楼-- · 2019-05-21 11:05

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?

查看更多
看我几分像从前
6楼-- · 2019-05-21 11:12

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.

查看更多
登录 后发表回答