john.tm has asked for the wisdom of the Perl Monks concerning the following question:

I have a perl Tk gui which is used to run a script i have got all the stdout and stderr from the script to print in the text widget. It also has a help button which inserts a txt file into the text widget. I im trying to get stderr messages from the gui itself to show in the text widget, for example if any of the 'or die' warnings are trigered due to files not being available the 'error' pop up box appears,but if there is an error opening the sub runscripts report.pl, the warning appears in the cmd prompt screen, i want it to appear in the gui popup warning box.
use warnings; use strict; use Tk; use POSIX 'strftime'; use Tk::ErrorDialog; my $DATE = strftime(" report.pl for %d %b %Y " , localtime()); my $title = strftime(" Processing daily Failures for %d %B %Y" , local +time()); my $mw = MainWindow->new; my $filenameA = "c:\\Temp\\perl.txt"; $mw->geometry("720x500"); $mw->title(" report "); my $main_frame = $mw->Frame()->pack(-side => 'top', -fill => 'x'); my $left_frame = $main_frame->Frame(-background => "snow2")->pack(-sid +e => 'left', -fill => 'y'); my $right_frame = $main_frame->Scrolled("Text", -scrollbars => 'se',-b +ackground => "black",-foreground => "green",-height => '44')->pack(-e +xpand => 1, -fill => 'both'); my $failures_button = $left_frame->Button(-text => "$DATE ", -command => [\&runscript])->pack; my $guide = $left_frame->Button(-text => " Clear Screen + ", -command => [\&clear_screen])->pack; my $Close_button = $left_frame->Button(-text => ' Exit + ', -command => [$mw => 'destroy'])->pack; my $Help_button = $left_frame->Button(-text => " Help Guide + ", -command => \&help_file)->pack(-side = +> "bottom"); my $About = $left_frame->Button(-text => ' About + ', -command => \&About_file)->pack(-side => "bottom"); + MainLoop; sub runscript { open (daily_fail, '-|', 'report.pl &') or die "unable to start dail +y_failues.pl"; my $first_line = " please wait......\n $title........\n"; $right_frame->delete("1.0", 'end'); $right_frame->insert( 'end', $first_line ); my $daily_fail_line; while (defined ($daily_fail_line =<daily_fail>) ) { $right_frame->insert( 'end', $daily_fail_line ); $right_frame->update(); $right_frame->see('end'); } } sub clear_screen { $right_frame->delete('1.0','end'); } sub About_file { $right_frame->delete("1.0", 'end'); open (FH, "$filenameA") or die "unable to open c:\\Temp\\perl.txt"; while (<FH>) { $right_frame->insert("end", $_); } close (FH); } sub help_file { system("tk.pl") or die "unable to start c:\\tk.pl"; }

Replies are listed 'Best First'.
Re: Perl Tk stderr to show in the Tk gui text widget
by kevbot (Vicar) on Jul 06, 2014 at 08:30 UTC

    You may want to take a look at the Tk::Stderr module.

    Tk::Stderr

    DESCRIPTION

    This module captures that standard error of a program and redirects it to a read only text widget, which doesn't appear until necessary. When it does appear, the user can close it; it'll appear again when there is more output.

Re: Perl Tk stderr to show in the Tk gui text widget
by Anonymous Monk on Jul 06, 2014 at 07:03 UTC

    open (daily_fail, '-|', 'report.pl &') or die "unable to start dail +y_failues.pl";

    See Proc::Background and Capture::Tiny, esp for win32, and then forward any stderr stuff up to tk as you please

      system("tk.pl") or die "unable to start c:\\tk.pl";

    Hmm, tk.pl and C:\tk.pl are different :)

Re: Perl Tk stderr to show in the Tk gui text widget
by zentara (Cardinal) on Jul 06, 2014 at 13:43 UTC
    in a strange quirk of fate, I happen to be experiencing the same problem, in Re: commandline ftpssl client ... a Tk frontend with Perl. I can plainly see the xterm is receiving more debug info than my Tk::Text widget watching the SDTOUT. In my case I think there is a low level system distinction between the tty output and stdout.

    So I think the problem comes down to finding a way of tying STDERR to STDOUT, within the code. Googling for perl tying stderr to stdout is giving me some ideas.

    For instance read: the basics of STDERR redirection especially the 1 reply.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh