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

Hello Monks

I have a problem handling an archive from within Perl (under Windows 7)

I have an archive and want to add a few files. The files to add are in a subdir which resides in $archivedir. The list of files is in @filelist (path relative to $archivedir). When I run it it creates arch.cpio.new but it remains empty and gives messages that it cannot find the files to add:

/tttest/cpio: test/1: Cannot stat: No such file or directory /tttest/cpio: test/2: Cannot stat: No such file or directory /tttest/cpio: test/3: Cannot stat: No such file or directory 1 block

The files are there...
Any suggestions?
Thank you.

My code: chdir($archivedir); my ($WRITEME); my $program= "| \"$bindir/cpio\" -o -O arch.cpio.new "; my $pid = open( $WRITEME, $program); binmode $WRITEME; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<solution foreach my $f (@filelist){print $WRITEME "$f\n";} close($WRITEME);

Replies are listed 'Best First'.
Re: perl, cpio, and pipe
by Anonyrnous Monk (Hermit) on Jan 13, 2011 at 15:06 UTC

    Just a wild guess (I don't have a Windows system / cpio here to try).

    Maybe cpio doesn't like the \r that the file handle's :crlf layer (by default active on Windows) is adding when you say print $WRITEME "$f\n";.

    At least on Unix I get similar stat errors when I deliberately add \r to the filenames piped to cpio — not sure, though, how a Windows version of cpio would handle carriage returns...

    In other words, try binmode $WRITEME.

      binmode was the solution! Thank you!
Re: perl, cpio, and pipe
by Corion (Patriarch) on Jan 13, 2011 at 13:39 UTC

    You don't check whether $archivedir exists and you can chdir into it. Use autodie or manually check for success:

    chdir $archivedir or die "Couldn't chdir into '$archivedir': $!";
      In the actual code I do check (I build the filelist between chdir and cpio). After modification to make it more similar to the example-code I can assure you that chdir($archivedir) is executed correctly.