Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I cant seem to get anything to print after I close my data output to file part.
open(FH, "> filename.txt") || die "Cant open file: $!\n"; select(FH); #rest of script here to print output to filename.txt close(FH); print "Nothing prints here after the close(FH)";
How do I get something to print after the "close(FH)"?
Right now it prints nothing on my NT command line.

Replies are listed 'Best First'.
Re: No print after closing FH
by particle (Vicar) on Jul 01, 2003 at 17:49 UTC

    select; as in:

    close FH; select STDOUT; print "...";

    ~Particle *accelerates*

Re: No print after closing FH
by Ovid (Cardinal) on Jul 01, 2003 at 17:52 UTC

    select returns the currently selected filehandle, so cache it and select it again:

    open FH, "> filename.txt" or die "Cannot open filename.txt: $!"; my $curr_fh = select (FH); # do your stuff; select $curr_fh; # now you can print!

    Cheers,
    Ovid

    Looking for work. Here's my resume. Will work for food (plus salary).
    New address of my CGI Course.

      thanks for quick answers. It now works!
Re: No print after closing FH
by BrowserUk (Patriarch) on Jul 01, 2003 at 17:51 UTC

    Add

    select(STDOUT);
    after you have closed(FH). Your leaving FH selected, so your prints are failing.

    If you had warnings enabled you would be seeing

    print() on closed filehandle FH at ....

    which is a really good reason for using it:)


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


A reply falls below the community's threshold of quality. You may see it by logging in.