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

Discussion in 'Perl Misc' started by John Davis, Sep 28, 2004.

  1. John Davis

    John Davis Guest

    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!
     
    John Davis, Sep 28, 2004
    #1
    1. Advertising

  2. John Davis wrote:
    > 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
     
    Gunnar Hjalmarsson, Sep 28, 2004
    #2
    1. Advertising

  3. John Davis wrote:
    > 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
     
    Jürgen Exner, Sep 28, 2004
    #3
  4. John Davis

    Shawn Corey Guest

    John Davis wrote:
    > 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)
     
    Shawn Corey, Sep 28, 2004
    #4
  5. John Davis

    Tintin Guest

    "John Davis" <> wrote in message
    news:...
    >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.
     
    Tintin, Sep 28, 2004
    #5
  6. John Davis

    Joe Smith Guest

    John Davis wrote:

    > 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
     
    Joe Smith, Sep 28, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stanley
    Replies:
    0
    Views:
    392
    Stanley
    Aug 29, 2003
  2. Kevin Spencer

    Re: Bad practice? Using #Include in ASP.NET

    Kevin Spencer, Aug 29, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    453
    Kevin Spencer
    Aug 29, 2003
  3. jm
    Replies:
    2
    Views:
    143
    kaeli
    Feb 9, 2004
  4. Replies:
    2
    Views:
    168
    Tad McClellan
    Mar 26, 2005
  5. jhumanski
    Replies:
    15
    Views:
    748
    Rainer Weikusat
    Apr 24, 2012
Loading...

Share This Page