roadtest has asked for the wisdom of the Perl Monks concerning the following question:
--here is my original question--
Hello, I am trying to copy multiple files to their corresponding output file as following:
It complains "Uncaught exception from user code:". If I don't use foreach loop, only copy one file as indicated in the commented out line. It is working fine.use warnings; use strict; use IO::File; use diagnostics; my @handles; my @temp; foreach my $file ( glob( '*.input' ) ) { (my $out = $file) =~ s/\.input$/.output/; push @handles, [ (IO::File->new('<$file') || die "die here\n"), (IO::File->new('>$out') || die "die under\n"), ]; } #push @handles, [ (IO::File->new('</etc/hosts') || die "die here\n"), +(IO::File->new('>/tmp/hostlist') || die "die under\n"), ]; while (@handles) { @handles = grep { if (defined(my $line = $_->[0]->getline)) { print { $_->[1] } $line; } else { 0; } } @handles; }
What is wrong with the loop? Thanks in advance!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: anonymous file handler objects
by moritz (Cardinal) on Dec 03, 2010 at 14:22 UTC | |
by roadtest (Sexton) on Dec 03, 2010 at 15:06 UTC | |
by moritz (Cardinal) on Dec 03, 2010 at 15:13 UTC | |
by roadtest (Sexton) on Dec 03, 2010 at 15:55 UTC | |
by mjscott2702 (Pilgrim) on Dec 03, 2010 at 16:26 UTC | |
by roadtest (Sexton) on Dec 03, 2010 at 19:18 UTC |