Lawrence Statton wrote:

Dear Guys,

I4ve been trying to develop an application that consists in about 8-10 scripts that should know about module variables (not OO yet). I read the docs and found that I can use a module variable (declared with our) in scripts that 'use' that module. Ok, there are:

module1.cgi   (declare and define var1)
main.cgi (declare and initialize var1)  "use module1"
script1.cgi (read and alter var1)       "use module1"
script2.cgi (gets var1 with undefined or previous value) "use module1"


I tried with our, importing that var and fully qualified it, but always it get the previous value. My question is: Which method is common to share data among apps? What Am I doing wrong? Also, I always use "strict" and "warnings".





Well, first -- I wouldn't name your modules *.cgi .... *.pm is what everyone expects to see, and in absence of a Good Overriding Reason you should follow the Principle of Least Astonishment. Naming your module "module" is also a terrible idea - unless you expect only to have one ever in your life. Give it a meaningful name.

That said:

The simplest way to create and use fully qualified variables from
another package is just to fully qualify them.

So, you've got some module Segrio's First Module:

                    SergioPrimo.pm
------------------------------------------------------------

package SergioPrimo;

our ($foo, $bar, $baz);

1;

------------------------------------------------------------

Now, down in your .cgi file, you can use those variables by their
names $SergioPrimo::foo $SergioPrimo::bar etc.

If you want not to have to fully qualify the variable name, you can
use Exporter ... something like


------------------------------------------------------------

package SergioPrimo;
use base 'Exporter';


our @EXPORT_OK = qw / $foo $bar $baz /;
our %EXPORT_TAGS = ( all => [EMAIL PROTECTED] );


our ($foo, $bar, $baz);

1;
------------------------------------------------------------

Now you can say down in your CGI

random.cgi
------------------------------------------------------------

use SergioPrimo (':all'); # import some variables

$foo = 123;  # sets $SergioPrimo::foo

------------------------------------------------------------

NOW ... all that being said:

It looks like your more serious problem is:  You're trying to store
state in Perl from different contexts, which is surely confusing you
no end.

If you're using just plain-old-CGI, then each and every time your web
server handles an inbound request, a brand new Perl interpreter is
instantiated, SergioPrimo::foo gets built fresh (as undef) and your
.cgi files may or may not set that variable and use them again later.

If you're using some persistent-perl-like product (e.g. FastCGI,
mod_perl) then you will find that SOMETIMES SergioPrimo::foo has the
last value it had, and sometimes it doesn't.

Perhaps you should take a step back and try to explain what you're
trying to do.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Lawrence Statton - [EMAIL PROTECTED] s/aba/c/g
Computer software consists of only two components: ones and
zeros, in roughly equal proportions. All that is required is to
sort them into the correct order.














Hi Lawrence,


It was a mistake. The module ends in .pm and not named "module", it was only to demonstrate. And yes, I'm trying to save state information across pages. I'll try a CGI::Session and, for now, I want only to thank all of you for the help.




Yours,


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to