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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

run program from a perl script

Status
Not open for further replies.

3inen

Technical User
May 26, 2005
51
US
Hello i downloaded a program fuzzyK and i want to run it through a perl script.

the program file name is aerie and i call it from the command line in LINUX using


bash-3.00# ./aerie
Aerie> load Datasettrunc.cdt
Loading TableData from Datasettrunc.cdt
File has 98 rows and 106 columns
Done Loading Datasettrunc.cdt
Aerie> fuzzy 120
K/3 = 40
Please enter a file name to save the data:sampleoutfile

the program runs......
Done Computing Fuzzy KMeans
67 unique centroids identified
Saving Prototypes
Saving Memberships
Saving Hard
Aerie> Aerie> exit
bash-3.00#

i looked at a similar post ( on this website) but the solution there did not work for me.

i have tried using that solution

open C, "|./aerie";
print C "load Datasettrunc.cdt fuzzy 120 sclustering";
close C;

and also tried using the

system("./aerie load Datasettrunc.cdt fuzzy 120 sclustering");


both of them stop at the

Aerie>

any help is appreciated.

thanks in advance



 
Are there any expect modules that work on windows with activestate?

I couldn't find any in the past and I ended up telneting to myself just to use Net::Telnet's print/waitfor routines.




~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
There are not (that i know of). However, Expect now works with cygwin.

I've thought creating such a module a few times now, but honestly don't have the time at the moment.

- Miller
 
Hi! MillerH is this how you want me to write it.

#!/usr/bin/perl -w
use Expect;

# create an Expect object by spawning another process
@parms = ("load Datasettrunc.cdt","fuzzy 120","sclustering");

my $exp = Expect->spawn(./aerie, @params)
or die "Cannot spawn $command: $!\n";
 
3inen,

Honestly, I'm not even attempting to figure out what your application interface is. I simply observed that you're attempting to interact with the secondary app (by passing an exit command and maybe others), and that means that you'll want a secondary layer in order to manage that interaction.

Observe the following example of two scripts I created:

Code:
[gray][i]# name: reverse.pl[/i][/gray]
[gray][i]#[/i][/gray]
[gray][i]# Script to simulate a secondary application.  Accepts word phrases[/i][/gray]
[gray][i]# and returns the string reverse.[/i][/gray]

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

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]<<'END_INTRO'[/red][red];[/red]
[purple]Mary had a little lamb[/purple]
[purple]Then the lamb ate Mary[/purple]
[purple]and it was little no more.[/purple]
[red]END_INTRO[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$choice[/blue] = [red]'[/red][purple][/purple][red]'[/red][red];[/red]
[olive][b]while[/b][/olive] [red]([/red][blue]$choice[/blue] ne [red]'[/red][purple]exit[/purple][red]'[/red][red])[/red] [red]{[/red]
	[black][b]print[/b][/black] [red]"[/red][purple]Reverse: [/purple][red]"[/red][red];[/red]
	[olive][b]for[/b][/olive] [red]([/red][red];[/red][red];[/red][red])[/red] [red]{[/red]
		[blue]$choice[/blue] = <STDIN>[red];[/red]
		[url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url] [blue]$choice[/blue][red];[/red]
		[olive][b]last[/b][/olive] [olive][b]if[/b][/olive] [blue]$choice[/blue] =~ [red]qr/[/red][purple]^[[purple][b]\s[/b][/purple][purple][b]\w[/b][/purple]]+$[/purple][red]/[/red][red];[/red]
		[black][b]print[/b][/black] [red]"[/red][purple]Reverse (requires basic phrase; exit to quit): [/purple][red]"[/red][red];[/red]
	[red]}[/red]
	
	[black][b]print[/b][/black] [url=http://perldoc.perl.org/functions/scalar.html][black][b]scalar[/b][/black][/url] [url=http://perldoc.perl.org/functions/reverse.html][black][b]reverse[/b][/black][/url] [blue]$choice[/blue][red];[/red] [gray][i]# Reverse $choice as a string[/i][/gray]
	[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

[black][b]print[/b][/black] [red]<<'END_SALUTATION'[/red][red];[/red]
[purple]Goodbye world![/purple]
[purple]It was nice knowing you...[/purple]
[purple]Come back again some time.[/purple]
[red]END_SALUTATION[/red]

[fuchsia]1[/fuchsia][red];[/red]

[teal]__END__[/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
[/tt]

Code:
[gray][i]# name: expect.pl[/i][/gray]
[gray][i]#[/i][/gray]
[gray][i]# Script to simulate interact with the reverse.pl script. Goal is:[/i][/gray]
[gray][i]# 1) Print character count of introduction[/i][/gray]
[gray][i]# 2) Reverse "A simple phrase";[/i][/gray]
[gray][i]# 3) exit[/i][/gray]
[gray][i]#[/i][/gray]
[gray][i]# Dependency: Expect[/i][/gray]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]Expect[/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]$exp[/blue] = Expect->[maroon]spawn[/maroon][red]([/red][red]"[/red][purple]perl reverse.pl[/purple][red]"[/red][red])[/red]
	or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Cannot spawn: [blue]$![/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[blue]$exp[/blue]->[maroon]log_stdout[/maroon][red]([/red][fuchsia]0[/fuchsia][red])[/red][red];[/red]

[blue]$exp[/blue]->[maroon]expect[/maroon][red]([/red][fuchsia]5[/fuchsia],
	[red][[/red] [red]qr/[/red][purple]Reverse: [/purple][red]/[/red] => [url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [red]{[/red]
		[black][b]my[/b][/black] [blue]$exp[/blue] = [url=http://perldoc.perl.org/functions/shift.html][black][b]shift[/b][/black][/url][red];[/red]
		[black][b]my[/b][/black] [blue]$intro[/blue] = [blue]$exp[/blue]->[maroon]before[/maroon][red];[/red]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Intro is: [/purple][red]"[/red] . [url=http://perldoc.perl.org/functions/length.html][black][b]length[/b][/black][/url][red]([/red][blue]$intro[/blue][red])[/red] . [red]"[/red][purple] characters[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
		[blue]$exp[/blue]->[maroon]send[/maroon][red]([/red][red]"[/red][purple]A simple phrase[purple][b]\n[/b][/purple][/purple][red]"[/red][red])[/red][red];[/red]
	[red]}[/red] [red]][/red],
[red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Expect timed out: [blue]$![/blue][/purple][red]"[/red][red];[/red]

[blue]$exp[/blue]->[maroon]expect[/maroon][red]([/red][fuchsia]5[/fuchsia],
	[red][[/red] [red]qr/[/red][purple]Reverse: [/purple][red]/[/red] => [black][b]sub[/b][/black] [red]{[/red]
		[black][b]my[/b][/black] [blue]$exp[/blue] = [black][b]shift[/b][/black][red];[/red]
		[black][b]my[/b][/black] [blue]$output[/blue] = [blue]$exp[/blue]->[maroon]before[/maroon][red];[/red]
		[blue]$output[/blue] =~ [red]s/[/red][purple][^[purple][b]\n[/b][/purple]]*[purple][b]\n[/b][/purple][/purple][red]/[/red][purple][/purple][red]/[/red][red];[/red] [gray][i]# Remove previous command[/i][/gray]
		[black][b]print[/b][/black] [red]"[/red][purple]Reverse is: [blue]$output[/blue][/purple][red]"[/red][red];[/red]
		[blue]$exp[/blue]->[maroon]send[/maroon][red]([/red][red]"[/red][purple]exit[purple][b]\n[/b][/purple][/purple][red]"[/red][red])[/red][red];[/red]
	[red]}[/red] [red]][/red],
[red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Expect timed out: [blue]$![/blue][/purple][red]"[/red][red];[/red]

[blue]$exp[/blue]->[maroon]soft_close[/maroon][red]([/red][red])[/red][red];[/red]

[fuchsia]1[/fuchsia][red];[/red]

[teal]__END__[/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Other Modules used :
[ul]
[li]Expect[/li]
[/ul]
[/tt]

And the output:

Code:
$ perl expect.pl 
Intro is: 76 characters
Reverse is: esarhp elpmis A

- Miller

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top