K
kj
One would think that one of the few good uses left for the keyword
"local" would be to "local-ly" close an open handle, like this:
my $status;
{
local *STDERR;
$status = system 'crazystuff';
}
die "can't do crazystuff!: $?" unless $status == 0;
But NOOOOO! The above fails to silence STDERR. If one attempts
a "close STDERR" after the "plain" local statement shown above,
one gets an error about attempting to close an unopened handle.
If then one tries this:
my $status;
{
local *STDERR = *STDERR; close STDERR;
$status = system 'crazystuff';
}
die "can't do crazystuff!: $?" unless $status == 0;
....then STDERR *is* finally silenced, but it *remains* silenced outside
of the block (e.g. the message from the die statement doesn't show);
i.e. the closing is not localized.
I know of ways to temporarily silence STDERR that actually work,
but none of them begins to approach the simplicity of any of the
examples above. Any suggestions?
TIA!
kj
"local" would be to "local-ly" close an open handle, like this:
my $status;
{
local *STDERR;
$status = system 'crazystuff';
}
die "can't do crazystuff!: $?" unless $status == 0;
But NOOOOO! The above fails to silence STDERR. If one attempts
a "close STDERR" after the "plain" local statement shown above,
one gets an error about attempting to close an unopened handle.
If then one tries this:
my $status;
{
local *STDERR = *STDERR; close STDERR;
$status = system 'crazystuff';
}
die "can't do crazystuff!: $?" unless $status == 0;
....then STDERR *is* finally silenced, but it *remains* silenced outside
of the block (e.g. the message from the die statement doesn't show);
i.e. the closing is not localized.
I know of ways to temporarily silence STDERR that actually work,
but none of them begins to approach the simplicity of any of the
examples above. Any suggestions?
TIA!
kj