in reply to (Ovid) Re: Using File Reference
in thread Using File Reference

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.

Replies are listed 'Best First'.
Re: Re: (Ovid) Re: Using File Reference
by danboo (Beadle) on Apr 05, 2002 at 20:29 UTC
    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