[Israel.pm] Re: Perl Digest, Vol 10, Issue 35

roiem at actcom.co.il roiem at actcom.co.il
Wed Mar 24 12:55:49 PST 2004


> > Name: From the Command Line to the Browser
> > 
> > Length: 20 minutes
> > 
> > Summary: will show a very experimental web development trick, that lets
> > you call web pages as subs.
> > 
> > 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?

Hmm, thinking out loud here, but what if you wrapped flow in something
like this: (pseudo-Perl here)

# Main program
$session=GetSessionID(); # Global variable holds the session ID
if (not exists $sessions{$session}) {
  run flow() in an external thread/process that stays alive when the
    HTTP request ends
} else {
  $current_prompt_answer=param('current_prompr_answer');
  if ($sessions{$session}{'last-prompt-given'} !=
      $current_prompt_answer) {
    # Error: someone went back in their browser or something like that
  }
  Send signal to external thread/process with the answer
  Wait for signal from external thread/process with the question
  Create new HTML page with $sessions{$session}{'last-prompt-given'} in
  a hidden form element
}

prompt() {
  $sessions{$session}{'last-prompt-given'}++
  Send signal to the main program with the question
  Wait for signal from the main program with the answer
  return $answer;
}

Of course, after calling flow() the external process will have to send
some "we're done" signal to the main program, but the idea is that the
main program goes up and down for each connection, but flow() stays up
al the time. (We could kill stale processes and stuff but that's for
later, I think)

Does that make any sense?

- Roie



More information about the Perl mailing list