[Israel.pm] Talk: A natural flow for web apps

Mikhael Goikhman migo at homemail.com
Tue Mar 23 16:16:03 PST 2004

On 24 Mar 2004 01:16:02 +0200, Ran Eilam wrote:
> > Consider a command line application that gets 2 numbers and 
> > prints their
> > sum:
> > 
> >   sub flow {
> >      my $a = prompt('Enter a number:');
> >      my $b = prompt('Enter another number:');
> >      prompt('Sum is: '. ($a + $b). '. Hit Enter to continue.');
> >   }
> > 
> > It is easy to write a prompt() that will do what we need.
> > Something like:
> > 
> >   sub prompt {
> >      my $message = shift;
> >      print "$message\n";
> >      chomp(my $out = <>);
> >      return $out;
> >   }
> > 
> > How do you write a prompt() that will allow flow() to run 
> > unchanged, but as a web app consisting of 3 pages?
> Why is it that every stupid little obfuscation gets the attention of
> many a mongers, yet when a REAL, profound, software engineering problem
> comes up (see below), it gets no answers at all? ;)

Personally I thought that you are going to show the bad and the good
solutions and we are just going to enjoy both of them. I also thought
that is just pity the talk is just 20 minutes. I didn't even think you
request some kind of answer. :)

Ok, the naive solution is to create web-app.cgi that is just like
original shell-app, just with prompt function replaced. (You may also
want to add BEGIN and END to output the header and footer html.)
This new prompt function may do something like:

	use CGI;
	use Util::FileSystem;

		our $currState = 0;
		our $lastState = param('state') || 0;
		my $value = param('var');
		if (defined $value) {
			saveFile("var-$lastState.txt", $value);

		print "<html>\n";
	END {
		print "</html>\n";

	sub prompt ($) {
		my $message = shift;
		if (++$currState <= $lastState) {
			return loadFile("var-$currState.txt");
		param('state', $lastState + 1);
		print start_form;
		print $message, br;
		print text_field('var');
		print hidden('state');
		print end_form;
		exit 0;                  # exit is important here

This may not even compile, who cares.

The idea is to ask a web visitor for input until everything is asked, and
store its sequential answers in the state files. The script is partially
executed again and again (why not?) on subsequential web-app.cgi calls
and exit in the middle printing a new prompt question. The only time our
web-app.cgi finishes its execution is when all questions were asked and
answered. We may want to print some greating message when this happens,
like: END { if ($currState == $lastState) { print "Thanks\n"; } }

Note that I didn't think for more than 20 minutes on this, and I typed
the solution most of this time, so don't expect quality.

> Maybe the silliness related posts are more gratifying? OK- I will buy a
> beer (or tea if we decide to move from the Godfather) for the 1st person
> who solves the problem below.

I prefer tea this time. :)


perl -e 'print+chr(64+hex)for+split//,d9b815c07f9b8d1e'

More information about the Perl mailing list