Discussion:
Using "system" command - is it "bad practice" for perl?
(too old to reply)
John Davis
2004-09-27 23:49:48 UTC
Permalink
A couple of quick questions, it is considered "bad" perl programming
(from a style perspective) to ever use the system() command to call
UNIX level commands in a script, or should the "backtick"/exec method
be used at all times (if possible).

I was working on a piece of code today and realized that after the
running the system commands as follows:

system "co -l /src/test-script2.pl"; # to checkout for RCS
system "vi /src/test-script2.pl"; # edit and process it and return
back

*** IT NEVER COMES BACK from the vi command!

system "ci -u /src/test-script2.pl";

foreach @x {
blah blah blah...
}

It did not return "control" back to my script and I had to Control-C
out of this script, and got frustrated at the system command. Is this
supposed to happen? Because in a ksh script, after doing a "vi", I
should get back to where I need to be.

Thanks!
Gunnar Hjalmarsson
2004-09-27 23:57:41 UTC
Permalink
Post by John Davis
A couple of quick questions, it is considered "bad" perl
programming (from a style perspective) to ever use the system()
command to call UNIX level commands in a script, or should the
"backtick"/exec method be used at all times (if possible).
Short answer: No.

When reading your message, I can't help getting the feeling that you
haven't studied the docs for system()

perldoc -f system

respective backticks (or the qx// operator)

perldoc perlop

Please do so, and come back if that does not answer you questions.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jürgen Exner
2004-09-28 00:02:04 UTC
Permalink
Post by John Davis
A couple of quick questions, it is considered "bad" perl programming
(from a style perspective) to ever use the system() command to call
UNIX level commands in a script, or should the "backtick"/exec method
be used at all times (if possible).
Is it bad considered bad style to ever use a hammer or should I always use a
wrench.

Well, dude, those are different tools. It depends on what you want to do.
Post by John Davis
I was working on a piece of code today and realized that after the
system "co -l /src/test-script2.pl"; # to checkout for RCS
system "vi /src/test-script2.pl"; # edit and process it and return
back
*** IT NEVER COMES BACK from the vi command!
Did you vi process terminate?

jue
Abigail
2004-09-28 00:00:48 UTC
Permalink
John Davis (***@onebox.com) wrote on MMMMXLV September MCMXCIII in
<URL:news:***@posting.google.com>:
`' A couple of quick questions, it is considered "bad" perl programming
`' (from a style perspective) to ever use the system() command to call
`' UNIX level commands in a script, or should the "backtick"/exec method
`' be used at all times (if possible).

No. system and backticks have different purposes. If you use backticks,
perl executes the given command *and collects its output*, returning
the output as a single scalar, or a list of scalars, depending on the
context. system, on the other hand, doesn't collect the output - it will
return the exit status of the process. system() also has a 'safe' mode,
which will prevent perl from using a shell. You can't do that with
backticks.

`' I was working on a piece of code today and realized that after the
`' running the system commands as follows:
`'
`' system "co -l /src/test-script2.pl"; # to checkout for RCS
`' system "vi /src/test-script2.pl"; # edit and process it and return
`' back
`'
`' *** IT NEVER COMES BACK from the vi command!

Huh? How so? It should come back whenever 'vi' is done (that is, when you
quit your editor). Just like it waits for any other program to finish.
If you want to run a different process *in parallel*, fork() and exec().

`' system "ci -u /src/test-script2.pl";
`'
`' foreach @x {
`' blah blah blah...
`' }
`'
`' It did not return "control" back to my script and I had to Control-C
`' out of this script, and got frustrated at the system command. Is this
`' supposed to happen? Because in a ksh script, after doing a "vi", I
`' should get back to where I need to be.


In Perl, it works the same way.


Abigail
--
perl -wlpe '}$_=$.;{' file # Count the number of lines.
Shawn Corey
2004-09-28 00:15:04 UTC
Permalink
Post by John Davis
A couple of quick questions, it is considered "bad" perl programming
(from a style perspective) to ever use the system() command to call
UNIX level commands in a script, or should the "backtick"/exec method
be used at all times (if possible).
I was working on a piece of code today and realized that after the
system "co -l /src/test-script2.pl"; # to checkout for RCS
system "vi /src/test-script2.pl"; # edit and process it and return
back
In UNIX, it's better to use:

my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
system "$editor /src/test-script2.pl";

VISUAL is the visual (screen) editor but many people set EDITOR as it.
EDITOR is the line editor. The emacs users will thank you.

--- Shawn
PS I use vim (VI Improved)
Tintin
2004-09-28 02:11:55 UTC
Permalink
Post by John Davis
A couple of quick questions, it is considered "bad" perl programming
(from a style perspective) to ever use the system() command to call
UNIX level commands in a script, or should the "backtick"/exec method
be used at all times (if possible).
They both do different things, so it depends on the circumstances.

On a general note, if you find your Perl script is full of system commands,
then you either aren't using the native Perl equivalents, or the script
would have been better written as a shell script.
Joe Smith
2004-09-28 06:39:42 UTC
Permalink
Post by John Davis
A couple of quick questions, it is considered "bad" perl programming
(from a style perspective) to ever use the system() command to call
UNIX level commands in a script, or should the "backtick"/exec method
be used at all times (if possible).
Use system() for anything that is interactive, like "vi", where the output
is expect to go directly to the screen (STDOUT).
Use backticks when you want to process the program's output yourself.

It's not a question of "bad" programming practice if you use the wrong
one; it's just that your program won't work with the wrong one.
-Joe

Continue reading on narkive:
Loading...