|
|
 |
 |
 |
 |
TCL(Tool Command Language) Scripting
|
 |
 |
 |
 |
 |
 |
 |
 |
Expect interaction with Perl
Hello, I'm not sure if this is a Perl question or an Expect question. I posted it to a Perl usenet group, but the only response I got so far was to suggest using the Expect module in Perl. I'd rather not do that, as the Expect script is already written and I'd prefer to use it rather than rewrite it in Perl Expect. I have written an Expect script that someone else is executing in their Perl script. I would like my Expect script to return a string as a value to this Perl script, not a boolean (i.e. integer). For example, say my Expect script does something like this: if (<this>) { return "SUCCESS"; } else { return "FAILURE"; }
The Perl script needs to have the value returned as either a "SUCCESS" or "FAILURE". I created a short Perl script that executes the Expect script by doing the following: my $rtnval = `rem.exp`; Of course, that's not going to return the value I am looking for; it's only going to print the output to stdout. And if I try: my $rtnval = system("rem.exp"); that's only going to tell me if the system call passed or failed (i.e. an integer), regardless of whether the Expect script deemed a success or fail. How can I call an Expect script from a Perl script and return a string value? Is there some trick to getting an interaction between Perl and Expect? I suppose I could do some kludge, like read the output into a file and search the file for the response. But I'd rather avoid that if possible. Thanks in advance to all that respond. Eric
In article <1171495592.114915.221@v45g2000cwv.googlegroups.com>,
Eric <ecarl @vmware.com> wrote: >Hello, >I'm not sure if this is a Perl question or an Expect question. I >posted it to a Perl usenet group, but the only response I got so far >was to suggest using the Expect module in Perl. I'd rather not do >that, as the Expect script is already written and I'd prefer to use it >rather than rewrite it in Perl Expect. >I have written an Expect script that someone else is executing in >their Perl script. I would like my Expect script to return a string as >a value to this Perl script, not a boolean (i.e. integer). >For example, say my Expect script does something like this: >if (<this>) { > return "SUCCESS"; >} else { > return "FAILURE"; >} >The Perl script needs to have the value returned as either a "SUCCESS" >or "FAILURE". >I created a short Perl script that executes the Expect script by doing >the following: >my $rtnval = `rem.exp`; >Of course, that's not going to return the value I am looking for; it's >only going to print the output to stdout. And if I try: >my $rtnval = system("rem.exp"); >that's only going to tell me if the system call passed or failed (i.e. >an integer), regardless of whether the Expect script deemed a success >or fail. >How can I call an Expect script from a Perl script and return a string >value? Is there some trick to getting an interaction between Perl and >Expect? I suppose I could do some kludge, like read the output into a >file and search the file for the response. But I'd rather avoid that >if possible.
. . . It's a Perl question, and frankly your Perl advisors seem to have led you astray--it's an *easy* Perl question. I'd love to chat at length, by the way, about the uses of scripting languages at VMWare. Briefly, you can't have it--or, more precisely, you're making things way too hard for yourself. Processes under Unix and Windows can emit strings to stdout, and they can return small integers as exit values, but they don't pass strings as exit values. They just don't. Unless you're in very unusual circumstances, pursuit of that is fruit- less. Focus on my $rtnval = `rem.exp`; and have rem.exp print its result to stdout. If I under- stand you correctly, that's going to give you the most satisfaction. Incidentally, Expect.pm is perfectly usable for cases such as yours.
At 2007-02-14 06:26PM, "Eric" wrote: [...] > For example, say my Expect script does something like this: > if (<this>) { > return "SUCCESS"; > } else { > return "FAILURE"; > } [...] > I created a short Perl script that executes the Expect script by doing > the following: > my $rtnval = `rem.exp`;
Perl's backticks capture the stdout of the called process, and $? captures the exit status. In your Expect script you need: if (<this>) { puts "SUCCESS"; } else { puts "FAILURE"; } -- Glenn Jackman "You can only be young once. But you can always be immature." -- Dave Barry
In article <0imca4-ksh.@lairds.us>, I blurted out: > . > . > . >It's a Perl question, and frankly your Perl advisors seem >to have led you astray--it's an *easy* Perl question.
. . . >Briefly, you can't have it--or, more precisely, you're >making things way too hard for yourself. Processes under >Unix and Windows can emit strings to stdout, and they can >return small integers as exit values, but they don't pass >strings as exit values. They just don't. Unless you're >in very unusual circumstances, pursuit of that is fruit- >less. >Focus on > my $rtnval = `rem.exp`; >and have rem.exp print its result to stdout. If I under-
. . . I've traded notes with Eric privately, and he seems to be on his way to satisfaction. I rushed when I posted earlier. I should amend a couple of points, so as not to lead others astray: A. It might be misleading to categorize this as a Perl matter rather than an OS-level one. In fact, wrappers in Perl, Python, C, Erlang, Rexx, ... would all naturally handle the IPC the same way: have the interior process write to stdout, and receive that "batched" result. B. If a person's working with an existing rem.exp, let's say, and wants to retain its behavior unchanged, it's perfectly fine to introduce a flag so that, in our example, the Perl wrapper invokes my $rtnval = `rem.exp -q`; and so on.
On Feb 15, 6:22 am, Glenn Jackman <gle@ncf.ca> wrote:
> At 2007-02-14 06:26PM, "Eric" wrote: > [...] > > For example, say my Expect script does something like this: > > if (<this>) { > > return "SUCCESS"; > > } else { > > return "FAILURE"; > > } > [...] > > I created a short Perl script that executes the Expect script by doing > > the following: > > my $rtnval = `rem.exp`; > Perl's backticks capture the stdout of the called process, and $? > captures the exit status. In your Expect script you need: > if (<this>) { > puts "SUCCESS"; > } else { > puts "FAILURE"; > } > -- > Glenn Jackman > "You can only be young once. But you can always be immature." -- Dave Barry
Thanks for your message, Glenn. puts will print to stdout, but so will send_user (although I realize there are differences between the two). Regardless of which one is used, it sounds as if the Perl script will need to read this value from stdout; is that what you are implying? Eric
Eric wrote: > On Feb 15, 6:22 am, Glenn Jackman <gle @ncf.ca> wrote: >> At 2007-02-14 06:26PM, "Eric" wrote: >> [...] >>> For example, say my Expect script does something like this: >>> if (<this>) { >>> return "SUCCESS"; >>> } else { >>> return "FAILURE"; >>> } >> [...] >>> I created a short Perl script that executes the Expect script by doing >>> the following: >>> my $rtnval = `rem.exp`; >> Perl's backticks capture the stdout of the called process, and $? >> captures the exit status. In your Expect script you need: >> if (<this>) { >> puts "SUCCESS"; >> } else { >> puts "FAILURE"; >> } >> -- >> Glenn Jackman >> "You can only be young once. But you can always be immature." -- Dave Barry > Thanks for your message, Glenn. puts will print to stdout, but so will > send_user (although I realize there are differences between the two). > Regardless of which one is used, it sounds as if the Perl script will > need to read this value from stdout; is that what you are implying? > Eric
Yep, only way to get strings from a subprocess is to read them from stdout, or else write/read them from a file. return values for *processes* can only be a number (and a relatively small one at that). Bruce
On Feb 15, 8:42 am, Bruce Hartweg <doNOT@nowhere.com> wrote:
> Eric wrote: > > On Feb 15, 6:22 am, Glenn Jackman <gle @ncf.ca> wrote: > >> At 2007-02-14 06:26PM, "Eric" wrote: > >> [...] > >>> For example, say my Expect script does something like this: > >>> if (<this>) { > >>> return "SUCCESS"; > >>> } else { > >>> return "FAILURE"; > >>> } > >> [...] > >>> I created a short Perl script that executes the Expect script by doing > >>> the following: > >>> my $rtnval = `rem.exp`; > >> Perl's backticks capture the stdout of the called process, and $? > >> captures the exit status. In your Expect script you need: > >> if (<this>) { > >> puts "SUCCESS"; > >> } else { > >> puts "FAILURE"; > >> } > >> -- > >> Glenn Jackman > >> "You can only be young once. But you can always be immature." -- Dave Barry > > Thanks for your message, Glenn. puts will print to stdout, but so will > > send_user (although I realize there are differences between the two). > > Regardless of which one is used, it sounds as if the Perl script will > > need to read this value from stdout; is that what you are implying? > > Eric > Yep, only way to get strings from a subprocess is to read them from > stdout, or else write/read them from a file. return values for > *processes* can only be a number (and a relatively small one at that). > Bruce- Hide quoted text - >
Thanks to everyone that responded. I was able to get something to work by using bacticks to capture the response of the Expect program and then parsing it using Perl. This was a much easier problem than I was making it. I guess I got caught up in the mindset of Perl-Expect interaction. Eric
In article <660Bh.1$%@dfw-service2.ext.ray.com>, Bruce Hartweg <doNOT@nowhere.com> wrote: >Eric wrote: >> On Feb 15, 6:22 am, Glenn Jackman <gle @ncf.ca> wrote: >>> At 2007-02-14 06:26PM, "Eric" wrote: >>> [...] >>>> I created a short Perl script that executes the Expect script by doing >>>> the following: >>>> my $rtnval = `rem.exp`; >>> Perl's backticks capture the stdout of the called process, and $? >>> captures the exit status. In your Expect script you need: . . . >> Thanks for your message, Glenn. puts will print to stdout, but so will >> send_user (although I realize there are differences between the two). >> Regardless of which one is used, it sounds as if the Perl script will >> need to read this value from stdout; is that what you are implying? >> Eric >Yep, only way to get strings from a subprocess is to read them from >stdout, or else write/read them from a file. return values for >*processes* can only be a number (and a relatively small one at that).
. . . A. Eric, please understand that backticks automate the reading. All the Perl side needs is the my $rtnval = `rem.exp` you appear already to have (maybe with a bit of chop()ping and such, depending on how you choose to handle newlines). B. As Bruce knows, there are LOTS of ways to get strings between processes <URL: http://wiki.tcl.tk/1228 >. Given the architecture you've specified, though (Perl wraps Expect as a subprocess, ...), indeed the "only way to get strings ..." is as we've described.
|
 |
 |
 |
 |
|