Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

anonymous file handler objects

by roadtest (Sexton)
on Dec 03, 2010 at 14:15 UTC ( #875190=perlquestion: print w/replies, xml ) Need Help??
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.


        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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://875190]
Approved by moritz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2017-11-25 12:13 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (355 votes). Check out past polls.