-----Original Message-----
From: $Bill Luebkert [mailto:[EMAIL PROTECTED]
Sent: Tuesday, May 17, 2005 8:38 PM
To: Haimov, Eugene
Cc: [email protected]
Subject: Re: Preserving State in CGI
Haimov, Eugene wrote:
> Note:
> I know about CGI::Session and about Storable class. But I want to
> dig deeper. Please read on.
>
> Hi everyone,
> I don't know how to explain the question in the commonly used terms,
> so let me start with an example.
>
> Imagine a CGI script that needs to serve several different pages
> in succession. Here is roughly how I would inplement it:
<snip>
> Note that the communication with the user happens everywhere,
> including inside the function invocations. The problem is that I want
> to convert such a program ( as in example 2 ) into a CGI script.
> Too bad -- because I have to "cut" it at every spot where the
> "$xxxx = <>" happens, and provide a huge if/elsif structure to
> manage the continuation of the execution ( as in example 1 ).
> Sometimes it is even impossible to "restore" the context -- as it is
> for instance inside the functions.
>
> The problem is not just the difficulty of doing it once.
> The problem is that if I actually cut the program into "pages",
> then it will lose its original structure completely, and it will be
> next to impossible to debug and further develop for those people
> who "understand" it in the original form.
>
> Any idea or trick that wold allow to preserve the structure and
> still make it a CGI script ? Something magical like
> "SaveStateCompleteIncludingCallStack()" ???
There are several different approaches you could take.
1) There could be different scripts for different sets of functions
and you could use cookies to pass some args/state if needed.
2) You could load a module or 'do' some code in for that specific
set of args (similar to 3 below codewise - except using 'do').
3) You could use a hash instead of if'ing all the possible args cases
if (exists $code{$arg}) { # where %code contains references to subs
to process each arg type
&$code{arg};
} else {
bad arg
}
4) There are some templating systems that may be useful for this sort
of thing.
------------------------------------------------------------------------
-----------
Bill,
Thanks for you suggestions.
Here is the way I though of doing it:
<snip>
print "here - before sub\n";
#---------------------------------
# replacement for "sub my_sub"
goto end_my_sub;
my_sub:
#---------------------------------
print "inside sub\n";
goto pop(@STACK); # replacement for "return"
#---------------------------------
# replacement for "}"
end_my_sub:
#---------------------------------
print "after sub\n";
print "Now calling sub\n";
#---------------------------------
# replacement for "my_sub()"
push ( @STACK, 'ret_here' );
goto my_sub;
ret_here:
#---------------------------------
print "finished\n";
</snip>
This way everything is "flat" from the Perl point of view, and so I
can pass the control into literally ANY point of the program, as
long as I have it labeled, and saved that label in the session.
To work in practive, this approach requires some pre-processing
to be done on the Perl source. I wonder if Perl has some facility
to plug-in such custom pre-processor (written in Perl). If not, it
will have to be a separate "step" from "source" to "executable".
Eugene Haimov
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs