bichonfrise74 has asked for the wisdom of the Perl Monks concerning the following question:
When I run this code, I get this error.#!/usr/bin/perl use warnings; use strict; open( my $output, '>', '/tmp/output.txt' ) or die "Error: Cannot open the file - $!\n"; open( my $input, '<', '/tmp/input.txt' ) or die "Error: Cannot open the file - $!\n"; print {$output} while (<$input>); ## This is were the problem is. close( $input ); close( $output );
This is referring to the line print {$output}...syntax error at test.pl line 12, near "} while" Execution of test.pl aborted due to compilation errors.
And running it, I get this on the standard output, and no output file is created.print $output while (<$input>);
Finally, when I change the line to this...GLOB(0x84f3bdc)GLOB(0x84f3bdc)....
Then everything is okay. My question is why do I have to explicitly write $_ in the above cases? I would assume that the content of $_ is changing, but I'm not sure where and I'm not sure if this assumption is even correct.print {$output} $_ while (<$input>); or print $output $_ while (<$input>);
#!/usr/bin/perl use warnings; use strict; open( OUTPUT, '>', '/tmp/output.txt' ) or die "Error: Cannot open the file - $!\n"; open( my $input, '<', '/tmp/input.txt' ) or die "Error: Cannot open the file - $!\n"; print OUTPUT while (<$input>); close( $input ); close( OUTPUT );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Printing with Indirect FileHandles
by ig (Vicar) on Aug 26, 2009 at 00:32 UTC | |
by ikegami (Patriarch) on Aug 26, 2009 at 00:54 UTC | |
by ig (Vicar) on Aug 26, 2009 at 01:03 UTC | |
by ikegami (Patriarch) on Aug 26, 2009 at 04:21 UTC | |
by bichonfrise74 (Vicar) on Aug 26, 2009 at 04:13 UTC |