From msemtd at yahoo.co.uk Wed Nov 1 05:47:19 2006 From: msemtd at yahoo.co.uk (Michael Erskine) Date: Wed, 1 Nov 2006 13:47:19 +0000 Subject: [Nottingham-pm] tied filehandle - how to get the filehandle from the PRINT method? Message-ID: <200611011347.19957.msemtd@yahoo.co.uk> 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? Regards, Michael Erskine. -- He is the best of men who dislikes power. -- Mohammed Send instant messages to your online friends http://uk.messenger.yahoo.com From djf at star.le.ac.uk Wed Nov 1 08:09:44 2006 From: djf at star.le.ac.uk (Duncan John Fyfe) Date: Wed, 01 Nov 2006 16:09:44 +0000 Subject: [Nottingham-pm] tied filehandle - how to get the filehandle from the PRINT method? In-Reply-To: <200611011347.19957.msemtd@yahoo.co.uk> References: <200611011347.19957.msemtd@yahoo.co.uk> Message-ID: <1162397384.2478.35.camel@saturn3.star.le.ac.uk> 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 From msemtd at yahoo.co.uk Thu Nov 2 04:12:21 2006 From: msemtd at yahoo.co.uk (Michael Erskine) Date: Thu, 2 Nov 2006 12:12:21 +0000 Subject: [Nottingham-pm] =?iso-8859-1?q?tied_filehandle_-_how_to_get_the_f?= =?iso-8859-1?q?ilehandle=09from_the_PRINT_method=3F?= In-Reply-To: <1162397384.2478.35.camel@saturn3.star.le.ac.uk> References: <200611011347.19957.msemtd@yahoo.co.uk> <1162397384.2478.35.camel@saturn3.star.le.ac.uk> Message-ID: <200611021212.22091.msemtd@yahoo.co.uk> On Wednesday 01 November 2006 16:09, Duncan John Fyfe wrote: > sub foo ... Thanks Duncan but I've just come across http://search.cpan.org/~reynolds/IO-Capture-0.05/ which contains IO::Capture::Stderr and IO::Capture::Stdout which are a better way of achieving my fancy-ass hack. Regards, Michael Erskine. -- Sattinger's Law: It works better if you plug it in. Send instant messages to your online friends http://uk.messenger.yahoo.com From msemtd at yahoo.co.uk Thu Nov 2 07:39:46 2006 From: msemtd at yahoo.co.uk (Michael Erskine) Date: Thu, 2 Nov 2006 15:39:46 +0000 Subject: [Nottingham-pm] =?iso-8859-1?q?tied_filehandle_-_how_to_get_the_f?= =?iso-8859-1?q?ilehandle=09from_the_PRINT_method=3F?= In-Reply-To: <200611021212.22091.msemtd@yahoo.co.uk> References: <200611011347.19957.msemtd@yahoo.co.uk> <1162397384.2478.35.camel@saturn3.star.le.ac.uk> <200611021212.22091.msemtd@yahoo.co.uk> Message-ID: <200611021539.47045.msemtd@yahoo.co.uk> On Thursday 02 November 2006 12:12, Michael Erskine wrote: > Thanks Duncan but I've just come across > http://search.cpan.org/~reynolds/IO-Capture-0.05/ which contains > IO::Capture::Stderr and IO::Capture::Stdout which are a better way of > achieving my fancy-ass hack. Even better, I've just got my head around tie and made a little package that is custom to my Tk needs... #!/usr/bin/perl -w use strict; use Tk; use Tk::Text; use Tk::ErrorDialog; package grabber; sub TIEHANDLE { my ($class,$textwidget, $tag) = @_; return bless {textwidget => $textwidget, tag => $tag}, $class; } sub PRINTF{ my $w = shift; $w->PRINT(sprintf(shift, at _)); } sub PRINT { my $obj = shift; my $tag = $obj->{tag}; my $w = $obj->{textwidget}; while (@_) { $w->insert('end',shift, $tag); } $w->see('end'); } package main; my $mw = new MainWindow(); my $tw = $mw->Text()->pack(); $tw->tagConfigure("stdout", -foreground => 'green'); $tw->tagConfigure("stderr", -foreground => 'pink', -background => 'red', -relief => 'raised', -borderwidth => 3); $mw->Button(-text => 'tie stdout', -command => sub { tie *STDOUT, 'grabber', $tw, 'stdout' })->pack(); $mw->Button(-text => 'tie stderr', -command => sub { tie *STDERR, 'grabber', $tw, 'stderr' })->pack(); $mw->Button(-text => 'talk stdout', -command => sub { print STDOUT scalar(localtime).": Hello stdout!\n";})->pack(); $mw->Button(-text => 'warn', -command => sub { warn scalar(localtime). ": Hello warning!\n";})->pack(); $mw->Button(-text => 'die', -command => sub { die scalar(localtime). ": Hello death!\n";})->pack(); MainLoop; Regards, Michael Erskine. -- Many pages make a thick book. Send instant messages to your online friends http://uk.messenger.yahoo.com