You could just pass around a string rather than a subref: my $handler = 'sub { my $arg = @_; do_something(); }';
vs my $handler = sub { my $arg = @_; do_something(); }; When you want to call it later on you do it like: eval($handler)->('foo'); vs $handler->('foo'); Garth On Wed, 2002-05-29 at 22:17, Ryan Parr wrote: > I never do give enough info on the first e-mail. Thank you for bearing with > me... > > What I mean is, if a request comes in for a certain form I would like to be > able to do something like this: > > my $form = &load_form($r); > $c{$session_id}->{handler} = $form->{handler}; # <-- this being a code > ref... > $r->send_http_header; > print $form; > > Then when the user completes the form and resubmits: > > my $handler = $c{$session_id}->{handler}; > $r->send_http_header; > print $handler->($r); > > This is definately simplified, but the idea is there. I would like to be > able to store anything that can be referenced and have it be available to > all processes. I would like to be able to dynamically create anonymous > subroutine handlers based on input and have them be active until the form is > submitted, at which time they are used to process the form then discarded. > > Is this something that can be accomplished? The global hash using Perl > aliasing > (http://thingy.kcilink.com/modperlguide/perl/Using_the_Perl_Aliasing_Feature > _.html) works beautifully, until of course the form is submitted to another > httpd process, and I'm hoping to not have to limit myself to just one child. > > Obviously this can't be serialized, but there has to be *some* way to do > this... > > -- Ryan > > > ----- Original Message ----- > From: "Ryan Parr" <[EMAIL PROTECTED]> > To: <[EMAIL PROTECTED]> > Sent: Wednesday, May 29, 2002 9:16 PM > Subject: Persistant references [was] Persistent Net::Telnet Objects > > > > Along these same lines I'm seeking a way to store a code reference into a > > global hash that is shared among all processes. For example: > > > > my $session_id = get_session_from_cookie($r); > > my $handler = $c{$session_id}->{handler}; > > > > $r->send_http_header; > > print $handler->($r); > > return OK; > > > > Has anyone performed this kind of magical tidbit before? Is there some > main > > process repository that I can access? > > > > -- Ryan > > > > > > ----- Original Message ----- > > From: "Rob Mueller (fastmail)" <[EMAIL PROTECTED]> > > To: "French, Shawn" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]> > > Sent: Wednesday, May 29, 2002 5:35 PM > > Subject: Re: Persistent Net::Telnet Objects > > > > > > > Our project needed persistent socket connections open as well. There is > > > supposed to be a standard mechanism to pass file descriptors between > unix > > > processes, though it's bugginess level depends on your OS. There is a > perl > > > module for this called Socket::PassAccessRights. So what you can do is > > > create a daemon process that just hangs round holding socket connections > > > open, like a socket cache basically, and passing them back and forth > > between > > > Apache processes based on some session ID or user ID or the like. > > > > > > Your daemon ends up looking something like this (with lots more error > > > checking of course) > > > > > > my %sockmap; > > > while (1) { > > > my $clientsock = $listen->accept(); > > > chomp(my $sessionid = <$clientsock>); > > > my $cachesock = ($sockmap{$sessionid} ||= opennewsock()); > > > Socket::PassAccessRights::sendfd(fileno($clientsock), > > fileno($cachesock)); > > > $clientsock->close(); > > > } > > > > > > And in your mod_perl code you do something like: > > > > > > my $serversock = IO::Socket::INET->new(Server => 'localhost', Port => > > > SOCKETPOOLPORT); > > > print $serversock $sessionid, "\n"; > > > my $Fd = Socket::PassAccessRights::recvfd(fileno($serversock)); > > > open(my $realsocket, "<&=$Fd"); > > > fcntl($realsocket, F_SETFD, 0); > > > my $ofh = select($realsocket); $| = 1; select ($ofh); > > > > > > If you do some experimenting, you'll get something that works, you'll > also > > > find lots of cases that don't. > > > > > > Rob > > > > > > ----- Original Message ----- > > > From: "French, Shawn" <[EMAIL PROTECTED]> > > > To: <[EMAIL PROTECTED]> > > > Sent: Thursday, May 30, 2002 3:53 AM > > > Subject: Persistent Net::Telnet Objects > > > > > > > > > > Vitals: > > > > Apache/1.3.20 (Win32) mod_perl/1.25_01-dev mod_ssl/2.8.4 > OpenSSL/0.9.6a > > on > > > > Windows 2000 with PHP 4.21 > > > > > > > > I am working on a project that requires me to have two telnet objects > > per > > > > user session opened, and accessible throughout the user's session. I > > have > > > > looked at Apache::Session and many other solutions but my problem is > > that > > > to > > > > keep a Net::Telnet object, I need to keep open sockets and > filehandles, > > so > > > I > > > > cannot serialize the object and store it in a database or file. > > > > > > > > Currently I have similar code working flawlessly: > > > > ### > > > > # "startup.pl" - called when apache starts (ie. PerlRequire > > > > "d:/Apache/conf/startup.pl") > > > > ## > > > > use MySite::Session; > > > > > > > > ### > > > > # "Session.pm" > > > > ## > > > > @EXPORT = qw( %sessionHash ); > > > > our %sessionHash; > > > > > > > > ### > > > > # "init_session.pl" - called IN MOD_PERL when a new session is > requested > > > > ## > > > > use MySite::Session; > > > > $sessionHash{$session_id . "_telnetObj"} = Net::Telnet->new(); > > > > > > > > ### > > > > # "dostuff.pl" - called IN MOD_PERL many time throughout the session > > > > ## > > > > use MySite::Session; > > > > my telnetObj = $sessionHash{$session_id . "_telnetObj"}; > > > > bless (\$telnetObj, "Net::Telnet"); > > > > > > > > Although this is working right now, I don't know enough [ anything? > :) ] > > > > about Apache or mod_perl to be sure that this will work in the future. > > > What > > > > I am really concerned about is that the telnetObj will only be > > accessible > > > > from scripts run by the same child process as that which created and > > saved > > > > it. > > > > > > > > Is there a better way to do this? > > > > > > > > Thanks, > > > > Shawn French > > > > > > > > > > > > > >