loris has asked for the wisdom of the Perl Monks concerning the following question:
Dear All,
I know I should be using Perl6::Form and not struggling with Perl5 formats, but bear with me.
I want to print something formatted to the terminal or to a file. This is what I came up with:
#!/usr/bin/perl use strict; use warnings; use English qw( -no_match_vars ); use Getopt::Long; my $file; my $file_name = 'delete_me.txt'; GetOptions('file|f' => \$file, ); my $greeting = "Hello World!"; format BLAH = Greeting: @<<<<<<<<<<<<<<<< $greeting . $FORMAT_NAME = "BLAH"; my $fh; if (defined($file)) { open($fh,'>',$file_name) or die "Couldn't open $file_name for writ +ing: $!"; select($fh); } #$FORMAT_NAME = "BLAH"; write(); if (defined($file)) { close($fh); }
The code above produces the error
Undefined format "$fh" called at ./test.pl line 32.
if called with ./test.pl -f. If I uncomment the line before the write(), the data is written to the file.
Why is this and is there a better idiom for switching between filehandles when using formats?
Thanks,
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Placement of $~ or $FORMAT_NAME
by JavaFan (Canon) on May 13, 2011 at 12:57 UTC | |
by loris (Hermit) on May 13, 2011 at 13:08 UTC | |
by Tux (Canon) on May 13, 2011 at 13:23 UTC |