Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Need suggestion for selecting IPC::Open3 or backticks

by techman2006 (Beadle)
on Nov 12, 2013 at 16:46 UTC ( #1062236=perlquestion: print w/replies, xml ) Need Help??

techman2006 has asked for the wisdom of the Perl Monks concerning the following question:

I am bit confuse to select out of IPC::Open3 or back ticks for running tar command (as need to archive a set of files). But also need to make sure that any abort operation should be avoided as once the tar operation is completed I need to perform some DB operations.

In the above approach if any error happens occur while archiving I just need to avoid the DB operation and may accept the abort operation request.

So what you suggest and if possible can I have a small example which I can use as reference.

  • Comment on Need suggestion for selecting IPC::Open3 or backticks

Replies are listed 'Best First'.
Re: Need suggestion for selecting IPC::Open3 or backticks
by kcott (Archbishop) on Nov 12, 2013 at 17:56 UTC

    G'day techman2006,

    Take a look at "perlop: Quote-Like Operators". Under qx/STRING/ (aka `STRING`), you'll find information on how to check for problems with whatever command you're running.

    Also, in "perlvar: Error Variables", under $CHILD_ERROR (aka $?), you'll see how to get the status of such commands.

    For running a simple tar command, IPC::Open3 doesn't seem appropriate; however, if you have more complex usage, an explanation of this would lead to a more informed answer.

    -- Ken

      G'day Ken,

      Thanks for the references. Let me try to explain the problem on which I am working.

      I need to scan a set of directory and based on certain criteria I may need to perform a tar operation. Once the operation is successful I need to update some entries in DB( a SQLite DB).

      Now the issue is say an user use Ctrl-C to abort the program. As it may come while DB operations going on it will result into a inconsistent state of DB (which need to be avoided). As if the program run again the flow will be decided based on the entries in DB.

      So there are two ways to handle such case

      1. Catch all such signals and let the program run

      2. Catch such signals and handle the condition gracefully.

      So I am thinking to take approach 2 for the same. So I was thinking how to achieve the same. Any example with this reference will be great help.

      Hope this will help.

        "perlipc: Signals" explains how to trap and handle signals.

        There's quite a few examples, including $SIG{INT} which is what you want for Ctrl-C.

        Make sure you read down to the caveat: "Be careful: qx(), system(), and some modules for calling external commands ..." - this also has example code.

        -- Ken

Re: Need suggestion for selecting IPC::Open3 or backticks
by zentara (Archbishop) on Nov 12, 2013 at 20:12 UTC
    I am bit confuse to select out of IPC::Open3 or back ticks for running tar command (as need to archive a set of files). But also need to make sure that any abort operation should be avoided as once the tar operation is completed I need to perform some DB operations.

    Hi, I'm not completely sure of what you are doing, but if you need to watch the tar command running, to check for an abort situation, I will point out 1 significant difference between backticks and IPC::Open3. Backticks will stop processing of your main script until the backtick completes, necessitating watching signals and return codes. Whereas IPC::Open3 will allow you to use select or IO::Select to watch the returns coming in, and even separate the stdout and stderr of the command.

    Just to demonstrate this capability of IPC::Open3, look at this:

    #!/usr/bin/perl use warnings; use strict; use IPC::Open3; use IO::Select; my $pid = open3(\*WRITE, \*READ,\*ERROR,"/bin/bash"); my $sel = new IO::Select(); $sel->add(\*READ); $sel->add(\*ERROR); my($error,$answer)=('',''); while(1){ print "Enter command\n"; chomp(my $query = <STDIN>); #send query to bash print WRITE "$query\n"; foreach my $h ($sel->can_read) { my $buf = ''; if ($h eq \*ERROR) { sysread(ERROR,$buf,4096); if($buf){print "ERROR-> $buf\n"} } else { sysread(READ,$buf,4096); if($buf){print "$query = $buf\n"} } } } waitpid($pid, 1); # It is important to waitpid on your child process, # otherwise zombies could be created.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Need suggestion for selecting IPC::Open3 or backticks
by kennethk (Abbot) on Nov 12, 2013 at 18:40 UTC
    There are a few questions you need to ask yourself w.r.t. what external call technology to pick (IPC::Open3, system, `STRING`, fork/exec...). What degree of control/sandboxing do you need to have over the process? What kinds of control do you want to have over STDOUT/STDERR? What do you want/need to do with exit codes? I personally almost always use fork and exec, because then I can implement timeouts pretty easily.

    Also, since you are working with tar, you may consider using Archive::Tar(CORE module) instead of command-lining it. That might make your error-handling dramatically more straight-forward.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1062236]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2023-09-28 16:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?