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

Dear Monks

I was just learning about formats and I was wondering if you could explain why the header in the code below displays in the file but not at the console

# open up MYFILE for writing open(MYFILE,">myfile.txt") or die "Can't open up myfile: $!\n"; my ($name,$age, $salary) = ('girl', '21', '12'); # now this line format will automatically apply to MYFILE format MYFILE = @>>>>>>>>>>>>>>>>>>>>>>>>>>>> @>>>>>>>>>>>>>> $name, $salary . format MYFILE_TOP = ----------------------------------- Name Salary ----------------------------------- . write MYFILE; select(STDOUT); $~ = "MYFILE"; write; #write to STDOUT
thanks

Replies are listed 'Best First'.
Re: format header not displayed at STDOUT
by jethro (Monsignor) on Dec 30, 2010 at 15:54 UTC

    Untested: You changed the name of the format for STDOUT to 'MYFILE' but not the name of the _TOP format, i.e. try adding $^ = "MYFILE_TOP";

      that was it - great