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

I threw together an htpassword updating util... it uses expect to run htpasswd.

The problem is, everytime a new instance of expect is created:
$htpasswd = "/usr/local/apache/bin/htpasswd $htpasswdfile $formuser"; ($htpass = Expect->spawn("$htpasswd")) || die "could not spawn progra +m, $!";
All the text that has been printed so far get's re-printed.
so, the following code:
------------------------------
use CGI qw(:standard); use Expect; print "Content-type: text/html\n\n"; print "test1\n"; $htpasswd = "/usr/local/apache/bin/htpasswd $htpasswdfile $formuser"; ($htpass = Expect->spawn("$htpasswd")) || die "could not spawn progra +m, $!"; print "test2\n"; exit;
-------------------------
The end user get's back:
-------------------------
test1 Content-type: text/html test1 test2
-------------------------

Any idea why, or how to get around this (besides putting all the prints after initiating expect, which is my current solution)

Thanks!

Replies are listed 'Best First'.
RE: expect.pm
by merlyn (Sage) on May 16, 2000 at 04:45 UTC
Re: expect.pm
by lhoward (Vicar) on May 16, 2000 at 04:15 UTC
    When I run your test under my environment (Perl 5.005_02 w/ Expect 1.07) it works properly (no re-echoing of earlier prints). Does the behavior you see only occur only when running as a CGI or when run from the command-line as well?
RE: expect.pm
by Anonymous Monk on May 16, 2000 at 05:04 UTC
    It only happens when run as a cgi script.
    When I run it from the command line, it works as expected and produces only:
    Content-type: text/html
    
    test1
    test2
    I've also tried getting rid of the 'use CGI' line... but that didn't make a difference.

    As far as using the perl module mentioned above... Firstly, I didn't know one existed, so I might look into that. I'm also familiar with using expect, I just can't figure out its odd behavior in this case. (and there's always more than one way to do it... and I already did it this way and it works.)
    I'm just really currious as to why it behaves this way, I can't figure out why it would do this.

    When run as a CGI script... does anyone else get the same odd results?
    Thanks,
    --
    Josh I.
      It works for me both ways. What platform are you using?
Re: expect.pm
by Anonymous Monk on May 16, 2000 at 18:27 UTC
    Running Solaris Intel here, with apache/1.3.12, and perl version 5.004_04 built for sun4-solaris. I guess I'll see if I can't reproduce this on some other platforms.
    Thanks
    --
    Josh I.
Re: expect.pm
by Anonymous Monk on May 16, 2000 at 19:03 UTC
    I get the same odd behavior from an Linux Intel box (RedHat 6.2), Apache 1.3.11, perl 5.005_03, Expect.pm-1.07
    I'm running the script as a plain old cgi script (not using mod_perl). I don't see why that would have anything to do with it... but how are you running it?
    Thanks
    --
    Josh I.
Re: expect.pm
by Anonymous Monk on May 16, 2000 at 22:35 UTC
    I get the same odd behavior from an Linux Intel box (RedHat 6.2), Apache 1.3.11, perl 5.005_03, Expect.pm-1.07
    I'm running the script as a plain old cgi script (not using mod_perl). I don't see why that would have anything to do with it... but how are you running it?
    Thanks
    --
    Josh I.
Re: expect.pm
by Anonymous Monk on Feb 10, 2016 at 18:42 UTC
    Got burned with this long time ago when writing CGIs in perl. You need to autoflush stdout. Like $|=1; . (At least whenever calling anything else from perl, otherwise the headers may get corrupted and the server will block the output with a 500 error.)