Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

mod_cgid, CGI::Fast share values between different scripts?

by Anonymous Monk
on Oct 09, 2012 at 05:48 UTC ( #997926=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hello monks,

i run an apache2 server with mod_fcgid and like to have a var i like to share between different scripts. let's assume i have this script:

#!/usr/bin/perl use CGI::Fast; our $x; while ( CGI::Fast->new() ) { print "Content-type: text/html\n\n"; print ++$x; }

this script is named a.cgi AND b.cgi. when i call it with http://url/a.cgi it will increment up the var $x on each instance of a.cgi, the same for b.cgi. but i like that the same variable $x (address in the shared mem or wherever) is incremented regardless if i call a.cgi or b.cgi. they should print out the value of the same address of $x.

this is for sharing database handlers as well as ssl session contexts and other informations which should be provided among different scripts.

i hope the question is clear? how can i manage to share persistent vars between instances of different scripts and not only between instances of the same script in a mod_fcgid / FCGI / CGI::Fast environment ?

greetings & thanks

Comment on mod_cgid, CGI::Fast share values between different scripts?
Download Code
Re: mod_cgid, CGI::Fast share values between different scripts?
by Anonymous Monk on Oct 09, 2012 at 06:41 UTC
    You can't, if you want to share values between programs, you have to use an intermediary, some kind of ipc, some kind of database, IPC::Sharelite, DB_File, DBD::SQLite, mysql, ...
      thanks for the quick response. it's a pitty. such things worked a bit better with mod_perl.
      here is how it works:
      #!/usr/bin/perl use CGI::Fast; use IPC::ShareLite; my $share = IPC::ShareLite->new( -key => 0xBADC0DE, -create => 'yes', -destroy => 'no', ); while ( CGI::Fast->new() ) { print "Content-type: text/html\n\n"; my $x = $share->fetch; $x++; $share->store($x); print $x; }
Re: mod_cgid, CGI::Fast share values between different scripts?
by thomas895 (Hermit) on Oct 09, 2012 at 07:22 UTC

    Your approach goes wrong on several levels.
    First of all, you're not supposed to store database handles as persistent variables. It's a bad, bad idea. Use Apache::DBI and mod_perl for that. If you do not want to use DBI directly, I believe DBIx::Class does persistence as well.
    Next, it won't work like you expect it to. Say that user 1 comes and accesses a.cgi. Along comes user 2 and accesses a.cgi as well. User 1 and 2 move on to b.cgi, but then user 1 decides to go back to a.cgi. But what user 1 sees is not their data -- it is that of user 2, if even that. This will not go well, and is dangerous for sensitive data.

    Instead, what it sounds like you want is a session with a storage backend. Consider CGI::Session, for example, or Apache::Session(X) for mod_perl.
    If you want to make something that will go beyond just simple CGI scripts and want something extensible and robust, consider Catalyst or Mojolicious(::Lite). Feeling adventurous? Try Combust, which powers many perlweb sites, IIRC. I personally prefer Catalyst, but you might like something else.

    HTH


    Update: Replaced <pre /> with <code />, because that did not look right at all.

    ~Thomas~
    confess( "I offer no guarantees on my code." );
      didn't i mentioned this is for a FastCGI environment? it's not a mod_perl environment and will not become one.

      and yes, i like to share the exact same data (memory) between different users and scripts.

      but thanks for your hints. IPC::ShareLite's shared mem does all i need.

        didn't i mentioned this is for a FastCGI environment? it's not a mod_perl environment and will not become one.
        I was providing an example that could potentially work for both environments, not just for you, but for anybody on the internet who happens to come across your question and my answer.
        That is beside the point anyways, as sharing variables willy-nilly and without care will lead to disastrous results. You don't have to believe me. Try it out for yourself. Don't take my word -- or anybody else's -- for what it's worth, I could be lying. This is the internet.

        ~Thomas~
        confess( "I offer no guarantees on my code." );

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://997926]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2014-07-25 11:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (170 votes), past polls