in reply to cat -ing Multiple files with Perl

Put your list of files in @ARGV and then use <>.

#!/usr/bin/perl use strict; use warnings; @ARGV = ('file2.txt', 'file4.txt', 'file15.txt'); open SEL, '>', 'selected.txt' or die $!; while (<>) { print SEL; }
--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re^2: cat -ing Multiple files with Perl
by radiantmatrix (Parson) on Dec 07, 2005 at 15:43 UTC

    To expand on that, I do something which combines files specified by a glob into another. It is called like:

    $ catfiles.pl *.txt *.html > mynewfile.txt
    And it looks like:
    #!/usr/bin/perl use strict; use warnings; local $|=1; #turn of buffering for ( map { glob $_ } @ARGV ) { open my $FH, '<', $_ or do { warn "Can't open '$_' for read, skipping: $!"; next; } while (<$FH>) { print $_ } close $FH; }

    Of course, on many systems cat *.txt *.html > mynewfile.txt works just fine, but this tool is useful because (a)I can extend it when I need to do more advanced work, and (b)Win32 doesn't have cat.

    <-radiant.matrix->
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    "In any sufficiently large group of people, most are idiots" - Kaa's Law
      Actually, Windows does have a cat equivalent, even though less powerfull. It is called type. Usage:
      $type /? Displays the contents of a text file or files. TYPE [drive:][path]filename $
      And of course you can redirect and/or append to files by using '>>' or '>' respectivelly.
      too bad you never actually ran your code!