Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using a variable for a file handle

Status
Not open for further replies.

jpasquini

Programmer
Apr 20, 2006
44
US
Hi All,

Has anyone ever tried to use a variable for a file handle, as in:

OPEN ($file, "/home/working/logname" )

where $file changes, meaning the handle can be 1, 2, 3, 4, 5 depending on what the variable $file is.



I have a program which opens a tailed log file, and works perfectly well the first time:

open (LOG, "tail -f -n 25 ${batch_script_directory}LOG_$_[0]|") or die "Tail file error: $OS_ERROR:\n\n${batch_script_directory}LOG_$_[0]";

Yet after closing LOG and reopening it for the 2nd time, I get this error:

"?Error: Can't locate object method OPEN via package Tk:Event:IO on program line"

I can't see why Perl would not care the first time through, but every time after even if you close and reopen filehandle LOG.

The only solution I can think of is to have LOG a variable and change it each time through. Been stuck on this searching the web for ideas for about three days.........



 
Folks,

You can save yourself days of frustration if you ever have to do this. I finally found this obscure page which talks about the problem (not in a regular but extended GOOGLE search):

%3CPine.LNX.4.10.10003301157380.392-100000@mojo.covalent.net%3E


Basically all I had to do was add this line before the OPEN statement:

untie *LOG;

and everything works fine.
Sorry, the title of this posting should be "?Error: Can't locate object method OPEN via package Tk:Event:IO on program line" but this made the file handle change unnecessary. If anyone knows anything further on this subject (besides the paragraph by Larry repeated about 100 times on as many sites), feel free to post, thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top