Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Working with TAR files

by SpaceCake (Initiate)
on May 24, 2004 at 13:43 UTC ( #355905=perlquestion: print w/replies, xml ) Need Help??

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

Friday I had some troubles about how to detect tar files in use, which are uploaded to the box with FTP. I had lots of input from monks here, al'do I did not realize any of they suggestions, they did give me an idea how to solve my problem. TAR does not give any output if extraction is successful, at least none that I know. So I thought about putting that in the variable and use that one. Unfortunately it does not work so good. Can you, perlmonks, take a look and enlighten my path ?

#! /usr/bin/perl -w
use Net::FTP;
use File::Copy;
my $sfile = "data*.tar"
my $dstftp = ""
chomp(my @files=`ls $sfile`);
foreach $files (@files) {
        copy("$files","work/$files") or die "Copy failed: $!";
        my $testit = exec "cd work/ ;tar xf $files ; cd ..";
        if ($testit == undef)
                chomp(my @lpfiles = `ls work/data_10*`);
                foreach $lpfiles(@lpfiles) {
                        $ftp = Net::FTP->new("$dstftp", Debug => 1)
                                or die "Cannot connect to host: $@\n";
                                or die "Cannot login \n ", $ftp->message;
                        $ftp->binary or die ("cant Binary \n");
                        $ftp->hash or die ("cant Hash \n");
                                or die "Cannot change working directory \n", $ftp->message;
                                or die "put failed \n", $ftp->message;
                else {
                	print "Nothing to do. No readable TAR files"

Replies are listed 'Best First'.
Re: Working with TAR files
by Abigail-II (Bishop) on May 24, 2004 at 14:11 UTC
    my $testit = exec "cd work/ ;tar xf $files ; cd .."; if ($testit == undef)
    Some problems here:
    • exec will typically not return, as it replaces the current program by another. Use system instead.
    • system will return whether the command ran successfully, but in the case of having separate statements, it will return the success of the last statement - the cd. Its return value is not something you are interested in - furthermore, there's no point of doing cd .. at the end. The command is running as a separate process, and changing its working directory at the end is pointless. Also, you'll do the tar even if the first cd fails. You could do: cd work && tar xf $files, which will only run the tar if the cd succeeds.
    • If a shell command runs successfully, false is returned - a true value indicates a failure of some kind. This might seem counterintuitive, but since there's only one false number (0), and many true numbers, and there are many reasons a command can fail, this has some merit.
    • Don't compare your variable with undef. If you want to check for definedness, use defined $variable. But in this case, just check for truthness.


Re: Working with TAR files
by borisz (Canon) on May 24, 2004 at 13:50 UTC
    Use  system instead of  exec. Also you can use glob instead of `ls ...`. And Archive::Tar is a good startingpoint for your tar issue.
      from Arch::Tar I dont know how to check if the archive is healty or not. Btw. what do you think about the rest of the code ? as the $testit should be the most important in this to determine if a TAR archive is going to be used or not.
Re: Working with TAR files
by Happy-the-monk (Canon) on May 24, 2004 at 13:49 UTC

    Oh yes: tar will return binary 0 on successfull termination.

      to my $testing is going to be 0 and not undef ?

        use backticks or qx() instead of system or exec. As perldoc -f system and same for exec will tell you, that's going to give you the result of the shell level command call, while system gives you the result of the call to system which is not what you want.

        Update: system, as borisz told us, is the call you're looking for, but be careful because end-of-Update:

        My GNU tar actually gives back the names of the extracted files on STDOUT, even when it is called with system, but that's not the behaviour I had expected. Maybe have a few test runs to see what your tar does.

        Update2: Yes, testing for undef works allright.

        Cheers, Sören

Re: Working with TAR files
by rjray (Chaplain) on May 25, 2004 at 01:08 UTC

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-06-25 16:08 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (83 votes). Check out past polls.