[Nottingham-pm] tied filehandle - how to get the filehandle from the PRINT method?

Duncan John Fyfe djf at star.le.ac.uk
Wed Nov 1 08:09:44 PST 2006


On Wed, 2006-11-01 at 13:47 +0000, Michael Erskine wrote:
> Hi all,
> 
> I often use tie to direct STDOUT and STDERR to a Tk::Text box (this is a built 
> in feature of Tk::Text). I'd like to alter STDERR output to make it more 
> noticeable so I've derived from Tk::Text and overridden PRINT but now I need 
> to know which filehandle I'm PRINTing to!
> 
> I scanned perltie but nothing stood out - any ideas out there?
> 

sub foo
{
	use Symbol;
	my $fh = shift;
	my $s = qualify(*$fh);
	if ($s =~ /STDERR/)
	{
		print "I have ($s) STDERR\n";
	}
	elsif ($s =~ /STDOUT/)
	{
		print "I have ($s) STDOUT\n";
	}
	elsif ($s =~ /STDIN/)
	{
		print "I'm sorry $ENV{USER} I can't let you do that.";
	}
	else 
	{
		print "I have ($s) something else\n";
	}
}

foo(\*STDERR);
foo(\*STDOUT);

#Might do but breaks on
open(FH,">&STDERR");
&foo(\*FH); # goes to the else clause.


> Regards,
> Michael Erskine.
> 
-- 
Duncan John Fyfe <djf at star.le.ac.uk>



More information about the Nottingham-pm mailing list