TIEHANDLE and deep recursion



Hi,

I was wondering of some kind soul could tell me what I am doing wrong in the
code below (one module, one test file, minimum case)

I can see why calling print $fh in "sub PRINT" is recursing - but I cannot
find out how to stop it!! Even copying the filehandle (a suggestion via
Google) seems to not work. I've played with "tied" and do not seem to be
able to find the magic to obtain the underlying filehandle so that calling
print() does not immediately redirect to sub PRINT() and thus recurse.



#### SafeFile.pm #####
package SafeFile;
use warnings;

sub TIEHANDLE
{
my ($self, $fh, @options) = @_;
my $data = {
fh => $fh,
@options,
};
return bless $data, $self;
}

sub FETCH {
my ($self) = @_;
return $self->{fh};
}


sub PRINT {
my $self = shift;
my $fh = *{$self->{fh}};
print $fh @_;
}

sub CLOSE
{
my ($self) = @_;
close $self->{fh};
}

sub safewrite {
my $path = shift;
open my $fh, '>', ;
tie *$fh, __PACKAGE__, *$fh,
(
path => $path,
);
return $fh;
}

1;
#### END SafeFile.pm #####

#### test #####
#!/usr/bin/perl
use warnings;

use SafeFile;

my $fh = SafeFile->safewrite('/tmp/wibble');
print $fh "Hello\n";
print $fh "World\n";
close $fh;
#### END test #####


% perl test
#### Result #####
Deep recursion on subroutine "SafeFile::PRINT" at SafeFile.pm line 23.
/bin/bash: line 1: 21037 Segmentation fault perl test

shell returned 139

Press ENTER or type command to continue

#### END Result #####


The purpose of this is to redirect a request to open ...., ">somefile"; to
open ..., ">somefile.tmp";

The filehandle $fh should be usuable normally with print etc.

On close($fh), somefile.tmp is closed, and rename()'d (an atomic operation
on Linux) to "somefile" - thus the target file is never in a half written
state at any point.

Any thoughts would be most welcome :)

Cheers,

Tim



--
Tim Watts
.



Relevant Pages

  • Re: Determine read/write status of filehandles connected to memory objects.
    ... sub can_write { ... my $fh = shift; ... A more specific test if the warning was indeed the text "Filehandle ...
    (comp.lang.perl.misc)
  • Re: Looking for code to test an 8088 emulator
    ... ;** Test SEC, CLC and CLV ... As you can see every single instruction is tested in some way. ... SUB AL,BH, SUB AL,CL etc. to be fine as well as they all use the same ... If nobody can provide me such a test file, I'll see if I write one ...
    (comp.lang.asm.x86)
  • RE: Copying data from External File
    ... existing selection is. ... data to the wrong cells on the test file. ... Sub AutoPopulateTest() ... TESTFILE = ActiveWorkbook.Name ...
    (microsoft.public.excel.programming)
  • Re: Randomly selecting a cell weighted on percentage
    ... I still get occasional #REF!'s with your array* formula when F9 was ... Here's my test file: ... Weighted Random Draw_1a.xls ... Sub abcd(), this part wasn't done. ...
    (microsoft.public.excel.programming)
  • Problem running row color macro
    ... I created a macro in VBA to color rows based on the value of column K in each ... It works using a test file with the required values typed in. ... Sub Update_Report_Colors ...
    (microsoft.public.excel.programming)