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

I have a general question about copying files. I have 5 rather large files that I need to merge into one. Does anyone know of any modules that will do this? I went to cpan but I did not quite find what I was looking for. Thanks for you help.


Replies are listed 'Best First'.
Re: File Copy/Merge Question
by thelenm (Vicar) on Feb 13, 2004 at 20:16 UTC
    What do you mean by "merge"? Do you just need to concatenate the files? If so, you don't need Perl for this. Use this on *nix:
    cat file1 file2 file3 file4 file5 > bigfile

    -- Mike

    XML::Simpler does not require XML::Parser or a SAX parser. It does require File::Slurp.
    -- grantm, perldoc XML::Simpler

Re: File Copy/Merge Question
by diotalevi (Canon) on Feb 13, 2004 at 20:05 UTC

    I don't know of a module but this is dirt easy to do with standard perl. Here's a quicky program for you. I tried to demonstrate good habits - 5.005 compliance, safe-two-arg open, localizing filehandles before use, platform independance with binmode, and checking system calls for errors.

    use strict; use vars qw( $DESTINATION @SOURCES ); $DESTINATION = pop @ARGV; @SOURCES = @ARGV; local *DEST; open DEST, "> $DESTINATION\0" or die "Can't overwrite $DESTINATION: $!"; binmode *DEST or die "Can't binmode $DESTINATION: $!"; for my $file_name ( @SOURCES ) { local *IN; open IN, "< $file_name\0" or die "Can't open $file_name for readin +g: $!"; binmode *IN or die "Can't binmode $file_name: $!"; local $/ = \ 8192; while ( my $chunk = <IN> ) { print DEST $chunk or die "Can't write to $DESTINATION: $!"; } close IN or warn "Can't close $file_name after reading: $!"; } close DEST or warn "Can't close $DESTINATION after writing: $!";
Re: File Copy/Merge Question
by BazB (Priest) on Feb 13, 2004 at 21:07 UTC

    File::MergeSort or File::Sort will allow you to merge files.

    Disclaimer: I maintain File::MergeSort, patches welcome :-)

    If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
    That way everyone learns.

Re: File Copy/Merge Question
by Art_XIV (Hermit) on Feb 13, 2004 at 21:13 UTC

    Here's the extra-quick but naughty version. It should work just peachy with concatenating text files.

    use strict; use warnings; my $destination = pop @ARGV; open OUT, ">$destination" or die "Couldn't create the concatenated file $destination: $!\n"; print OUT while (<>); close OUT;

    The '<>' is a spicy bit of Perl that uses your command-line args as file names.

    Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"
      That's actually rather unsafe. See Dangerous diamonds!. This is why I was using the "< $file\0" form. If I were coding to 5.8 then I'd use three-arg open. You also just destroyed any binary data if this is to be done on a Windows computer.
Re: File Copy/Merge Question
by BrowserUk (Patriarch) on Feb 13, 2004 at 21:46 UTC

    This one liner will do it. If you have 5.8.1 or later, it is also safe to use on binary files on win32.

    perl -C3 -pe1 file1 file2 file3 > all

    And it's a lot easier to type than copy file1/b + file2/b + file3/b all/b which is the native command for this.

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Timing (and a little luck) are everything!