in reply to Using File Reference

I don't see any immediate error in your code, but here's a couple of things to think about. First, check the status of your open. If you failed to open the file, printing to the file will silently fail.

sub setDestFile { my($self, $file) = @_; local(*F); open( F, ">$file") or die "Cannot open $file for writing: $!"; $fh = \*F; $self->{'destfile'} = $fh; }

Next, munge your contructor to print a header right away. If the open is successful and you don't print a header (you can add an a die to print, also), then you know where to look for the problem.

Lastly, in &output, print $text somewhere where you can see it (STDOUT or STDERR come to mind) in order to verify that you are actually passing something.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re: (Ovid) Re: Using File Reference
by Anonymous Monk on Apr 05, 2002 at 18:26 UTC
    This is the entire test program I am using:
    #!/usr/bin/perl package Test; use strict; sub new { my $class = shift; return bless {'destFile' => \*STDOUT}, $class; } sub open { my($self, $file) = @_; local(*F); open(F, ">$file") or die "cannot open $file for writing: $!"; $self->{destFile} = \*F; } sub output { my($self, $text) = @_; my $fh = $self->{destFile}; print "$text\n"; printf {$self->{destFile}} "$text\n"; } sub close { my $self = shift; close( $self->{destFile}); } package main; $a = Test->new; $a->open("HELLO"); $a->output("THIS IS THE LINE TO OUTPUT TO THE FILE"); $a->close; exit;
    This program will create the HELLO file in the current directory but it WILL NOT print the output line to it. ?????? IS there anything wrong here.
      Add a use warnings pragma to your program and you'll see that the file handle, Test::F, is not open at the time you try to write to it.

      It is closing automatically as it goes out of scope. I think what you want is just a typeglob, not a reference to it. Simply remove the \ from before the typeglob and all is fine.

      $self->{destFile} = *F;
      Cheers,

      - danboo