Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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!

Comment on anonymous file handler objects
Download Code
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://875190]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2014-12-21 00:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (100 votes), past polls