CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Undefined Format

 



pmkenned
New User

Jan 3, 2012, 5:17 AM

Post #1 of 3 (1057 views)
Undefined Format Can't Post

Hi,

I have a very simple file:

#!/usr/bin/perl
use strict;
use format;

my $foo = 2;

format LIST =
@<<<<
$foo
.

open(FILE, ">foo.txt");
$~ = 'LIST';
print FILE "hello?";
write FILE;

-----

For whatever, reason I am getting the following error message:
Undefined format "FILE" called at ./test.pl line 16.

I am using Perl version 5.005_03 built for x85_64-linux.
I also get this error when running version 5.10.1 built for i686-cygwin-thread-multi-64int.

The odd thing is that it prints to the file using "print" just fine, but it cannot perform the "write". So, I know the problem is not with the file.
When I change the FILE to STDOUT, it prints to the screen successfully, so I know the problem is not with the format.

It seems to be only when I attempt to write a format to a file that there is a problem.

Any ideas? Thanks in advance.


rovf
Veteran

Jan 3, 2012, 7:03 AM

Post #2 of 3 (1054 views)
Re: [pmkenned] Undefined Format [In reply to] Can't Post

(1) You didn't indicate, which line is line 16 in your listing.

(2) What is "use format" supposed to do?

(3) Always put "use warnings" in your code.


BillKSmith
Veteran

Jan 3, 2012, 7:47 AM

Post #3 of 3 (1052 views)
Re: [pmkenned] Undefined Format [In reply to] Can't Post

$~ only works for the selected device. STDOUT is the default selection. That is why your script works for STDOUT. The easy fix is to change the name of the format to FILE.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives