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

replace '<$file' with "<$file" fix my problem. Thanks a lot for your suggestion.

--here is my original question--

Hello, I am trying to copy multiple files to their corresponding output file as following:

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; }
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.

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
      Yes, I have many market related *.input files under one directory. Try to use perl to back them up.

      Thanks,

        Update - sorry, forgot to hit "Create" and moritz had already backed up their original response with what I wrote below - feel free to ignore or delete!

        As moritz was pointing out, using the single quotes as in '<$file' would NOT interpolate, and would only work if you actually had a file called $file.