Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Funky file writing -- betcha you can't figure this one out 1

Status
Not open for further replies.

Nebbish

Programmer
Apr 7, 2004
73
US
I'm stumped (again):

Code:
open(OUTPUT, "> tester.txt");
print OUTPUT "hello\n";

open(STDERR, "> tester.txt");
die "123456789ThisIsMyErrorMessage";

This produces a file containing the following:

Code:
hello
89ThisIsMyErrorMessage at C:\ProjectsListExport\Scripts-7-15-04\webExporterModules\printLog.pm line 9.

No, I didn't leave out the 1-7 by accident--it's really not there. Fooling around with buffering ($| = 1) doesn't help. Where did the rest of my error message go?

-Nick
 
Try this -

open(OUTPUT, "> tester.txt");
print OUTPUT "hello\n";
open(STDERR, ">&OUTPUT");

die "123456789ThisIsMyErrorMessage";

It makes STDERR a duplicate of OUTPUT

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
It works (and I'll use it, thank you), but why didn't my original code work?
 
I'm no perl guru but I don't think the underlying OS will like the fact that you opened tester.txt twice. With I/O buffering who knows wher the seven chars went!


Columb Healy
Living with a seeker after the truth is infinitely preferable to living with one who thinks they've found it.
 
columb is quite right, you can't open a file twice - you have to duplicate the file handle.

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
And another quick thought
seven chars = hello\r\n if your OS is windows based, if you're *nix or Mac then that theory goes out of the window

Columb Healy
Living with a seeker after the truth is infinitely preferable to living with one who thinks they've found it.
 
Still isn't quite working. The buffering appears to be off:

Code:
$| = 1;

open(OUTPUT, "> tester.txt");
open(STDERR, ">&OUTPUT");

print OUTPUT "Here is my first statement.\n";
die "Second statement";

Produces

Code:
Second statement at C:\ProjectsListExport\Scripts-7-15-04\webExporterModules\printLog.pm line 9.
Here is my first statement.

Note that buffering is turned off ($| = 1), but the death happens before the print. Hmmmmmm.....ideas?

-Nick
 
you're not select ing the filehandle before you turn off buffering so the "buffering" setting is probably applying to STDOUT, also - try setting $| after you dupe the filehandle.

so after the two open lines

select OUTPUT; $|=1;

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
I had never seen select before, that did the trick. I kind of assumed $| affected the buffering for all filehandles. Good to know it doesn't.

Thanks,

Nick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top