Using "system" command - is it "bad practice" for perl?

J

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
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!
 
G

Gunnar Hjalmarsson

John said:
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.
 
J

Jürgen Exner

John said:
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.
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!

Did you vi process terminate?

jue
 
S

Shawn Corey

John said:
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

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)
 
T

Tintin

John Davis said:
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.
 
J

Joe Smith

John said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top