invoking system commands from a perl script

Discussion in 'Perl Misc' started by vabby, Mar 10, 2007.

  1. vabby

    vabby Guest

    Hi
    I want to know what is the diff b/w the following two ways of invoking
    system commands from a perl script:
    1) system(" rm abc.txt");
    2)`rm abc.txt';

    also of I mix and match the two styles, will i land into trouble, eg
    if i have to create a file and in the next step delete it, which one
    of the two ways will land me into trouble:
    1)
    system ("touch abc.txt");
    'rm abc.txt';

    or
    2)

    `touch abc.txt`;
    system("abc.txt");
     
    vabby, Mar 10, 2007
    #1
    1. Advertising

  2. vabby

    -berlin.de Guest

    vabby <> wrote in comp.lang.perl.misc:
    > Hi
    > I want to know what is the diff b/w the following two ways of invoking
    > system commands from a perl script:
    > 1) system(" rm abc.txt");
    > 2)`rm abc.txt';


    Read "perldoc system" and the section about "qx" in "perldoc perlop".
    The relationship is discussed in these documents.

    > also of I mix and match the two styles, will i land into trouble, eg
    > if i have to create a file and in the next step delete it, which one
    > of the two ways will land me into trouble:
    > 1)
    > system ("touch abc.txt");
    > 'rm abc.txt';
    >
    > or
    > 2)
    >
    > `touch abc.txt`;
    > system("abc.txt");


    None of these will necessarily land you in trouble, except with Perl
    stylists. Using `` in void context is always bad style.

    Anno
     
    -berlin.de, Mar 10, 2007
    #2
    1. Advertising

  3. vabby wrote:
    >
    > I want to know what is the diff b/w the following two ways of invoking
    > system commands from a perl script:
    > 1) system(" rm abc.txt");


    perldoc -f system

    > 2)`rm abc.txt';


    perldoc -f qx


    > also of I mix and match the two styles, will i land into trouble, eg
    > if i have to create a file and in the next step delete it, which one
    > of the two ways will land me into trouble:
    > 1)
    > system ("touch abc.txt");
    > 'rm abc.txt';
    >
    > or
    > 2)
    >
    > `touch abc.txt`;
    > system("abc.txt");


    How about using perl to do it (this is after all a Perl group.)

    open my $fh, '>', 'abc.txt' or die "Cannot open 'abc.txt' $!";
    close $fh or die "Cannot close 'abc.txt' $!";

    unlink 'abc.txt' or die "Cannot unlink 'abc.txt' $!";




    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, Mar 10, 2007
    #3
  4. vabby wrote:
    > I want to know what is the diff b/w the following two ways of invoking
    > system commands from a perl script:
    > 1) system(" rm abc.txt");
    > 2)`rm abc.txt';


    See the readily available documentation for each command:
    perldoc -f system
    perldoc -f perlop (and see the section about quotes and quote-like
    operators)

    > also of I mix and match the two styles,


    Those are not different styles. Those are two different commands that simply
    do different things.

    > will i land into trouble, eg
    > if i have to create a file and in the next step delete it, which one
    > of the two ways will land me into trouble:


    It's like asking if you can mix multiplication and addition in the same Perl
    program.
    They just do different things. Choose the one that is the right for your
    task.

    > system ("touch abc.txt");


    You should check if your call to system was successful.

    > 'rm abc.txt';


    I suppose you mean
    `rm abc.txt`;
    What output do you expect from the rm(1) command? Why are you asking for the
    output if you don't capture it?

    jue
     
    Jürgen Exner, Mar 10, 2007
    #4
  5. vabby

    Dr.Ruud Guest

    -berlin.de schreef:

    > Read "perldoc system"


    ITYM: perldoc -f system
    or: perldoc -q system

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Mar 10, 2007
    #5
  6. vabby

    vabby Guest

    so using backticks or system is teh same thing except when you want to
    capture the result value of teh command. my idea of asking the
    question was that somebody told me , that using system to execute unix
    commands, is the synchronous way of doing it, and using backticks the
    asynchronous way
     
    vabby, Mar 11, 2007
    #6
  7. vabby

    -berlin.de Guest

    vabby <> wrote in comp.lang.perl.misc:
    > so using backticks or system is teh same thing except when you want to
    > capture the result value of teh command.


    Its *output*. The term "result value" is ambiguous at best.

    > my idea of asking the
    > question was that somebody told me , that using system to execute unix
    > commands, is the synchronous way of doing it, and using backticks the
    > asynchronous way


    If by "synchronous" you mean you don't get control back before the
    command has been executed, both are synchronous. To get asynchronous
    behavior, in the sense that your Perl program keeps running while the
    external program executes, you'll need forking. That can happen
    directly or through appropriate calls of open().

    Anno
     
    -berlin.de, Mar 11, 2007
    #7
  8. vabby

    Uri Guttman Guest

    >>>>> "a" == anno4000 <-berlin.de> writes:

    a> vabby <> wrote in comp.lang.perl.misc:
    >> so using backticks or system is teh same thing except when you want to
    >> capture the result value of teh command.


    a> Its *output*. The term "result value" is ambiguous at best.

    >> my idea of asking the
    >> question was that somebody told me , that using system to execute unix
    >> commands, is the synchronous way of doing it, and using backticks the
    >> asynchronous way


    a> If by "synchronous" you mean you don't get control back before the
    a> command has been executed, both are synchronous. To get asynchronous
    a> behavior, in the sense that your Perl program keeps running while the
    a> external program executes, you'll need forking. That can happen
    a> directly or through appropriate calls of open().

    well, you know both system and backticks fork too. the synchronous part
    is that they wait until they complete. open | is one way to run an async
    process and IPC::eek:pen2/3 are some others. and as you implied calling
    fork (and maybe exec) directly is another.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Mar 11, 2007
    #8
  9. vabby

    Jamie Guest

    In <>,
    "vabby" <> mentions:
    >so using backticks or system is teh same thing except when you want to
    >capture the result value of teh command. my idea of asking the
    >question was that somebody told me , that using system to execute unix
    >commands, is the synchronous way of doing it, and using backticks the
    >asynchronous way


    Backtics are a sort of hackish way to get the output of the command (output
    as in, standard output, NOT the result)

    They're like the shell counterparts. In a standard shell, you can:

    -------------- shell ----------
    TOP=`pwd`
    echo $TOP
    ------------------------------


    In perl, you can also do this:

    open(PIPE,"pwd |");
    while(<PIPE>){
    ...
    }
    close(PIPE);

    Have to be a little careful with backtics. (actually, I sort of wish perl
    didn't have them as they are confusing with ')

    To get the "result", (error code) you want to look at the $? variable, sorta
    like this:

    my $rc = ($? >> 8);


    They all involve forking (at least on a unix platform) the difference is,
    things like backtics or system will wait() for the process to terminate
    before returning control back to you. This is generally what you want, if
    your task can't resume until something is done by the system.

    fork() and open(PIPE,"command_name |"); will launch the process and then
    return to you before the command completes. (in the above case, close()
    waits for the command to complete) This allows you to process the data
    from the shell command "as it happens" as opposed to "all at once".


    Jamie
    --
    http://www.geniegate.com Custom web programming
    Perl * Java * UNIX User Management Solutions
     
    Jamie, Mar 12, 2007
    #9
  10. vabby

    Uri Guttman Guest

    >>>>> "J" == Jamie <> writes:

    J> Backtics are a sort of hackish way to get the output of the command (output
    J> as in, standard output, NOT the result)

    hackish??? they are a perfectly normal and proper way to get all the
    stdout of a subprocess.

    J> They're like the shell counterparts. In a standard shell, you can:

    they were totally borrowed/stolen from the shell. like many other perl
    features were.

    J> Have to be a little careful with backtics. (actually, I sort of
    J> wish perl didn't have them as they are confusing with ')

    so use qx() instead. hard to confuse those with single quotes.


    J> To get the "result", (error code) you want to look at the $?
    J> variable, sorta like this:

    J> my $rc = ($? >> 8);

    why sorta?? i wouldn't call that the "result" either as few programs use
    the exit code (not error code) for real communication. a typical
    subprocess called under qx will just send output to stdout.

    J> They all involve forking (at least on a unix platform) the difference is,
    J> things like backtics or system will wait() for the process to terminate
    J> before returning control back to you. This is generally what you want, if
    J> your task can't resume until something is done by the system.

    J> fork() and open(PIPE,"command_name |"); will launch the process and then
    J> return to you before the command completes. (in the above case, close()
    J> waits for the command to complete) This allows you to process the data
    J> from the shell command "as it happens" as opposed to "all at once".

    and in most common subprocess cases there won't be much difference. the
    choice is more about how much output is generated (due to buffering
    needs) and how long the subprocess takes to run. some bigger differences
    is whether the shell is invoked in the middle and that open |'s exit
    code is returned by close. and you still need the same >>8 stuff to get
    the "exit" code.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Mar 12, 2007
    #10
  11. vabby

    Joe Smith Guest

    vabby wrote:
    > somebody told me , that using system to execute unix
    > commands, is the synchronous way of doing it, and using backticks the
    > asynchronous way


    Both are synchronous unless the shell metacharacter "&" is used for background
    execution. Using "&" with backticks is not very useful.
    -Joe
     
    Joe Smith, Mar 13, 2007
    #11
    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. dpackwood
    Replies:
    3
    Views:
    1,829
  2. Jim Fisher

    Invoking Commands in J2EE

    Jim Fisher, May 22, 2004, in forum: Java
    Replies:
    0
    Views:
    357
    Jim Fisher
    May 22, 2004
  3. Rob Cowie
    Replies:
    5
    Views:
    439
    Will McDonald
    Dec 17, 2005
  4. Mohd Ebrahim
    Replies:
    2
    Views:
    934
    Cubaman
    Jul 12, 2010
  5. martin
    Replies:
    1
    Views:
    129
    Jürgen Exner
    Apr 17, 2006
Loading...

Share This Page