selecting STDOUT AND STDERR

B

Bill Parker

I'm used to doing this

exec >>${LOG} 2>&1

In ksh and bash

And I know in perl I can do

open(LOG,">>logfile");

$stdout = select(LOG)
print "something\n";
select($stdout);

But can I exactly mimic the ksh/bash redirect of stderr as well as stdout?
And do I need to? Does perl distinguish betwixt stdout and stderr? What does
"die" write to? (s'pose I ought to test that out and find out for myself) -
I guess I'm concerned about my silently-running script failing in some way
and anything going to stderr not being picked up in the logfile...

My script is actually running on Windoze using WInXP's awful task scheduler
rather than cron so I can't redirect via the calling command... and anyway,
I prefer my scripts to be fully in control of things - I don't like hunting
down a long complicated chain of things that affect my proggies' execution.

As ever, all help gratefully received.

Cheers

Bill
 
T

Tad McClellan

Bill Parker said:
$stdout = select(LOG)
print "something\n";
select($stdout);


What does that get you?

Why not just

print LOG "something\n";

instead, with no select()ing required?

But can I exactly mimic the ksh/bash redirect of stderr as well as stdout?
^^^^^^

perldoc -q stderr

How can I capture STDERR from an external command?

And do I need to?


Only you can answer that, as it depends on what you *want* to happen...

Does perl distinguish betwixt stdout and stderr?


Of course it does.

What does
"die" write to?


The very first sentence of

perldoc -f die

answers that.

You should read the documentation for the functions that you use.

(s'pose I ought to test that out and find out for myself) -


No, you should rather read the documentation for the software that you use.
 
B

Bill Parker

$stdout = select(LOG)
What does that get you?

Why not just

print LOG "something\n";

instead, with no select()ing required?

Because I don't necessarily know what is going to be output during the run
of the proggie, or what subcalls are going to generate any output. And as I
want it to write ALL it's output to a file, rather than (in my case anyway)
get lost by Windoze, a global redirect of STDOUT (and STDERR) is a
cheap'n'easy way for me to achieve it. It's a trick I use on shell
scripting, which also helps me keep the code looking a bit neater...
^^^^^^

perldoc -q stderr

Hey, this works on Activestates perl and Windoze. Impressed am I! Thanks for
this tip. Complicated looking output - I may have to turn my brain on to
handle it :-} I need to make more use of perldoc, obviously.
The very first sentence of

perldoc -f die

answers that.

Umm, not that I yet understand why but Activestate requires me to to

perldoc.bat -f die

before it works. But again, good tip. I DID actually mean "try and browse
the docm" rather than being lazy and stating "I'll test it" - as I said, me
being lazy over my typing.

Many thanks, very helpful

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top