jfrai has asked for the wisdom of the Perl Monks concerning the following question:

I have several Perl scripts I use to perform installations of various tools. To avoid command line mistakes, I've been reading through PerlTk documentation and have fumbled my way to a point where I have a decent GUI with nice little checkboxes for tool selection and some path selections. Lo and behold, I even have the background Perl scripts working with the GUI to properly install the programs.

However, all the output from the Perl scripts go to the cmd window that the PerlTk main window was initially launched from. I would like to have the command windows (for the child Perl scripts) opened in a new frame, with the resulting commands and output directed into that window. I've not been able to find a decent example of this.

I attempted something like the following, obviously simplified for doing a simple "dir," but I couldn't seem to get any output. Eventually, I would like to compile this so that I don't have to rely on TCL and Perl dependencies, so getting the output into a frame is important for the GUI.

use strict; use Tk; use Tk::Button; use Tk::Checkbutton; use Tk::LabEntry; use Tk::Label; my $cmdLineText; my $mw=MainWindow->new(-title=>'Foo Installer'); my $row = 0; my $w_install = $mw-> Button ( -overrelief=>'raised', -relief=>'raised +', -text=>'Install', -command=>\&installItems) -> grid(-column=>0, -r +ow=>++$row, -columnspan=>3, -sticky=>'nsew'); my $w_cmdWindow = $mw->Label (-height=>7, -foreground=>'white', -backg +round=>'black', -text => $cmdLineText)->grid(-column=>0, -row=>++$row +, -columnspan=>3, -rowspan=>10, -sticky=>'nsew'); MainLoop; sub installItems { open (FILE, "dir 2>&1 |"); #appears to do nothing $cmdLineText = ""; while (<FILE>) { chomp; $cmdLineText .= $_; } close(FILE); #$cmdLineText = `dir`; #appears to do nothing #system "dir"; #outputs to original screen }
Thanks for whatever clues/examples you can provide.

Update1: The first answer that mentioned using "cmd.exe /c ..." works well for Windows commands, like "dir," but unfortunately doesn't handle output from Perl scripts in the same way. Thank you, though. That's another tool for my toolbag.

Update2: I attempted to use the "tie" procedure but couldn't seem to get that to work properly. I'll look into IPC::Run.

  • Comment on How can I redirect Windows command line output into a PerlTk frame
  • Download Code

Replies are listed 'Best First'.
Re: How can I redirect Windows command line output into a PerlTk frame
by frieduck (Hermit) on Mar 05, 2009 at 01:28 UTC
    dir 2>&1 doesn't work in Windows. cmd.exe /c dir does.
    Following is my updated version. I also had to change -text => $cmdLineText to -textvariable => \$cmdLineText for the Label to see the changes in the variable.
    use warnings; use strict; use Tk; use Tk::Button; use Tk::Checkbutton; use Tk::LabEntry; use Tk::Label; my $cmdLineText; my $mw=MainWindow->new(-title=>'Foo Installer'); my $row = 0; my $w_install = $mw-> Button ( -overrelief=>'raised', -relief=>'raised +', -text=>'Install', -command=>\&installItems) -> grid(-column=>0, -r +ow=>++$row, -columnspan=>3, -sticky=>'nsew'); my $w_cmdWindow = $mw->Label (-height=>7, -foreground=>'white', -backg +round=>'black', -textvariable => \$cmdLineText)->grid(-column=>0, -ro +w=>++$row, -columnspan=>3, -rowspan=>10, -sticky=>'nsew'); MainLoop; sub installItems { open (FILE, "cmd.exe /c dir|"); #does something $cmdLineText = ""; while (<FILE>) { #chomp; $cmdLineText .= $_; } close(FILE); }
Re: How can I redirect Windows command line output into a PerlTk frame
by KSURi (Monk) on Mar 05, 2009 at 08:55 UTC
    You can redirect output to a text widget by tie()ing:
    my $text = $mw->Text(-width => 10, -height => 10)->pack; tie *STDOUT, ref $text, $text;
    Maybe this will help you
      You can redirect output to a text widget by tie()ing:

      Does this solution work on Win32?

      I ask because I would assume that the tie solution would itself rely upon the use of non-blocking filehandles and select for its internal operation, and as these do not work on Win32 filehandles...


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re: How can I redirect Windows command line output into a PerlTk frame
by zentara (Cardinal) on Mar 05, 2009 at 12:18 UTC
Re: How can I redirect Windows command line output into a PerlTk frame
by BrowserUk (Patriarch) on Mar 05, 2009 at 16:15 UTC