Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl Tk to provide a graphic interface for windows command programs 1

Status
Not open for further replies.

svar

Programmer
Aug 12, 2001
349
GR
I have a windows excecutable, which itself accepts more commands
The issue is automating it via perl, e.g.

c:>myprogram
myprorgam# myprogram_command1
result1:a
result2:b
myprogram# myprogram_command2 a b (the output of the previous command)
...

I can do in perl system "myprogram.exe",
but the question is how to capture the result of the myprogram commands.

Any ideas?
 
I can do in perl system "myprogram.exe",
but the question is how to capture the result of the myprogram commands.
use the open function.
For example to process the output of dir *.pl command in windows I can do the following:
popen.pl
Code:
[COLOR=#804040][b]use strict[/b][/color];
[COLOR=#804040][b]use warnings[/b][/color];

[COLOR=#804040][b]open[/b][/color]([COLOR=#008080]DIROUT[/color], [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]|dir *.pl[/color][COLOR=#ff00ff]"[/color]) [COLOR=#804040][b]or[/b][/color] [COLOR=#804040][b]die[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]Can't start DIR [/color][COLOR=#008080]$![/color][COLOR=#ff00ff]"[/color];
[COLOR=#804040][b]while[/b][/color] ([COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$line[/color]=[COLOR=#008080]<DIROUT>[/color]) {
  [COLOR=#804040][b]chomp[/b][/color]([COLOR=#008080]$line[/color]);
  [COLOR=#008080]&process_line[/color]();  
}
[COLOR=#804040][b]close[/b][/color]([COLOR=#008080]DIROUT[/color]);

[COLOR=#0000ff]#----------------------[/color]

[COLOR=#804040][b]sub[/b][/color][COLOR=#008080] [/color][COLOR=#008080]process_line[/color][COLOR=#008080] [/color]{
  [COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$line[/color] = [COLOR=#008080]@_[/color];
  [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#008080]$line[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
}
Output:
Code:
D:\Work>popen.pl
 Volume in drive D has no label.
 Volume Serial Number is B407-A942

 Directory of D:\Work

08. 09. 2009  16:24               468 multiline.pl
06. 08. 2009  17:57               335 mysyscmd.pl
16. 04. 2010  13:03               263 popen.pl
...
 
NOt sure it works

my $command=system 'c:\myprogram';
open(DIROUT, "|$command") or die "Can't start DIR $!";
....(rest is the same)

c:>\popen.pl

###Welcome to myprogram #############
myprogram # mycommand1
result of mycommand1 is:
1 3 joe mary
myprogram # quit
Bye :
'0' not recognized as internal or external command, excecutable program or ...

What I would like is to grab
the result of mycommand1 and pass it to a say mycommand2 also run from myprogram
 
mikrom, I think that should actually be 'dir *.pl|', because you want to read the output of the command, not supply the input. It only appears to work because the dir *.pl output goes to standard out anyway.

For svar's purposes though, he (or she?) will need to both write input to myprogram and read it in again, so I believe he will need to use IPC::Open2.

Here is a simple example that sends an IP address to nslookup.exe and reads the name that is returned for that address:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]IPC::Open2[/green][red];[/red]
[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$pid[/blue] = [maroon]open2[/maroon][red]([/red]\[blue]*CHLD_OUT[/blue], \[blue]*CHLD_IN[/blue], [red]'[/red][purple]nslookup[/purple][red]'[/red][red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] CHLD_IN [red]"[/red][purple]127.0.0.1[purple][b]\n[/b][/purple]exit[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[olive][b]while[/b][/olive] [red]([/red]<CHLD_OUT>[red])[/red] [red]{[/red]
	[olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple]^Name: *(.*)[/purple][red]/[/red][red])[/red] [red]{[/red] [black][b]print[/b][/black] [red]"[/red][purple]the name was [blue]$1[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red] [red]}[/red]
[red]}[/red]

Annihilannic.
 
if I try 'dir *.pl|' I get only zeros:
Code:
D:\Work>popen2.pl
0
0
0
...
 
That is because you are calling &process_line with no parameters, then assigning the array @_ to the scalar $line, in which case it is assigned the number of elements in @_, i.e. 0.

A corrected version would be:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]

[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red]DIROUT, [red]"[/red][purple]dir *.pl|[/purple][red]"[/red][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't start DIR [blue]$![/blue][/purple][red]"[/red][red];[/red]
[olive][b]while[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$line[/blue]=<DIROUT>[red])[/red] [red]{[/red]
  [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red]([/red][blue]$line[/blue][red])[/red][red];[/red]
  [maroon]&process_line[/maroon][red]([/red][blue]$line[/blue][red])[/red][red];[/red]
[red]}[/red]
[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url][red]([/red]DIROUT[red])[/red][red];[/red]

[gray][i]#----------------------[/i][/gray]

[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]process_line[/maroon] [red]{[/red]
  [black][b]my[/b][/black] [red]([/red][blue]$line[/blue][red])[/red] = [blue]@_[/blue][red];[/red]
  [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$line[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

Annihilannic.
 
Hi Annihilannic,
I overlooked that. Thanks for the correction!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top