Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > global variables in a web service

Reply
Thread Tools

global variables in a web service

 
 
sal.x.lopez@gmail.com
Guest
Posts: n/a
 
      06-28-2006
How do I set global variables in a web service script so that they are
available to all functions? In my code below, and when called by the
client, the variable $str is empty within the helloWorld subroutine.

#!/bin/perl

use SOAP::Transport::HTTP;
SOAP::Transport::HTTP::CGI
->dispatch_to('DEMO')
->handle;

package DEMO;

my $str = "Hello World";

sub helloWorld {

my ($arg1, $arg2) = @_;

print "$str\n";

# do other stuff

}

 
Reply With Quote
 
 
 
 
Dodger
Guest
Posts: n/a
 
      06-29-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> How do I set global variables in a web service script so that they are
> available to all functions? In my code below, and when called by the
> client, the variable $str is empty within the helloWorld subroutine.
>
> #!/bin/perl
>
> use SOAP::Transport::HTTP;
> SOAP::Transport::HTTP::CGI
> ->dispatch_to('DEMO')
> ->handle;
>
> package DEMO;
>
> my $str = "Hello World";
>
> sub helloWorld {
>
> my ($arg1, $arg2) = @_;
>
> print "$str\n";
>
> # do other stuff
>
> }


For that case, use our() not my(), or predeclare the variable.

our() creates a package global.
This is not necessarily a true global, especially in a webserver
environment. That's probably what you want. I know you asked for a true
global, but I have some doubt as to whether you really want a true
global -- such would be shared with other requests and things and could
be bad: consider:

User 'schmoo' logs in and has the $handle variable set. You've made
this a true global.
Another user comes in and doesn't log in. The script is set to say
'Welcome, Guest' if the $handle variable isn't set or 'Welcome $handle'
if it is. You'd expect the second user to see 'Welcome Guest' but if
nothing specifically unsets $handle from being 'Schmoo' and $handle is
a true global, the unlogged in user will see 'Welcome Schmoo' instead.
This could be bad if, for instance, you have private information,
administrative functions, or consumer downloadables, for instance.

Secondly, such true globals will only be true to the server process
that they start in. Sone most webservers have anywhere form 5 to 500
child processes running to handle the traffic at one time, those would
be all entirely seperate processes.

So yeah, it's probably a package global you want, and therefore
our $variable = 'FooBar'

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      06-29-2006
(E-Mail Removed) <(E-Mail Removed)> wrote:

> How do I set global variables



"Coping with Scoping":

http://perl.plover.com/FAQs/Namespaces.html


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      06-29-2006

Quoth "Dodger" <(E-Mail Removed)>:
>
> For that case, use our() not my(), or predeclare the variable.
>
> our() creates a package global.
> This is not necessarily a true global, especially in a webserver
> environment.


Please explain what you think a 'true' global is if a package variable
isn't one.

Ben

--
Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
(Ted Hughes, [ Heracles shoots Vulture with arrow. Vulture bursts into ]
'Alcestis') [ flame, and falls out of sight. ] (E-Mail Removed)
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      06-29-2006

(E-Mail Removed) wrote:
> How do I set global variables in a web service script so that they are
> available to all functions? In my code below, and when called by the
> client, the variable $str is empty within the helloWorld subroutine.
>
> #!/bin/perl
>
> use SOAP::Transport::HTTP;
> SOAP::Transport::HTTP::CGI
> ->dispatch_to('DEMO')
> ->handle;
>
> package DEMO;
>
> my $str = "Hello World";
>
> sub helloWorld {
>
> my ($arg1, $arg2) = @_;
>
> print "$str\n";
>
> # do other stuff
>
> }


You should always enable warnings.

I'm guessing you are running this under mod_perl. If so you should see
your logs full of ..

Variable "..." will not stay shared

This is because Perl doesn't properly implement nested subroutines in
the was that, say, Pascal does and mod_perl wraps you script in "sub
handler {...}".

For details look up the above warning message in perldiag.

My perfered solution is to use package variables and dynamic scoping.

Yes I realise that there are issues with package variables and dynamic
scoping but this really is one of those cases where the disadvantages
are minimal. Would still advise all to understand the semantics of
package variables before usign them. See the local() and our()
functions.

I perfer to say "package variables" not "global variables" because as
the OP correctly implied the file-scoped lexical is also a global
variable of sorts from the design point of view.

When using CGI scripts under mod_perl simply replace "my" by "local
our" for those variables declared in the outermost scope which trigger
the will not stay shared
warning.

Note this is still a somewhat dirty hack. Consider instead moving DEMO
into a separate ,pm file or writing a real mod_perl handler rather than
using one of the CGI emmulation modes.

 
Reply With Quote
 
Dodger
Guest
Posts: n/a
 
      07-07-2006

Ben Morrow wrote:

> Please explain what you think a 'true' global is if a package variable
> isn't one.


While I was honestly tempted to just say 'no' and to qualify that by
stating that I don't *think* I *know*...

http://perldoc.perl.org/perlvar.html

They're in there.

 
Reply With Quote
 
Dodger
Guest
Posts: n/a
 
      07-07-2006

Though admittedly, I was more referring to the fact that registry
scripts under mod_perl are evaled and thus sort of run in a sandbox of
sorts, where to reach things in Main:: you have to do so explicitly --
and usually you don't really want to.

 
Reply With Quote
 
David Combs
Guest
Posts: n/a
 
      07-11-2006
In article <(E-Mail Removed) .com>,
Brian McCauley <(E-Mail Removed)> wrote:
>

....
>
>My perfered solution is to use package variables and dynamic scoping.


"Dynamic scoping" -- nice terminology.

Why? Because (I assume) lots of computer-sci books on
general language implementation (and explanation) use it;
it is (or was) something pretty much everyone who in
cs courses studied languages like lisp and its relatives
and children.

Larry, of course, has chosen the seemingly opposite-meaning
term "local" for the concept in perl.

Sure would be nice if for p6 he'd switch to the more-generally
(across cs) terminology: people coming to perl from other
(interpretive) languages would know instantly what
it meant.

Unfortunately, those same people, seeing "local",
*also* think they understand *that*!


David
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      07-11-2006
David Combs <(E-Mail Removed)> wrote:

> "Dynamic scoping" -- nice terminology.


> Larry, of course, has chosen the seemingly opposite-meaning
> term "local" for the concept in perl.



The biggest cause of the local() confusion is that "local" is short
for something, and most folks fillin the wrong something.

local() makes a "local value", not a "local variable".


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Stephan Titard
Guest
Posts: n/a
 
      07-12-2006
Tad McClellan escribió:
> David Combs <(E-Mail Removed)> wrote:
>
>> "Dynamic scoping" -- nice terminology.

>
>> Larry, of course, has chosen the seemingly opposite-meaning
>> term "local" for the concept in perl.

>
>
> The biggest cause of the local() confusion is that "local" is short
> for something, and most folks fillin the wrong something.
>
> local() makes a "local value", not a "local variable".
>
>

yes. Around here I tell people it is short for "localize"

hth
--steph
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does timer in Web Service Global.asax block my Web Service from processing web-site requests? Leo Violette ASP .Net Web Services 0 04-17-2009 12:39 AM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
InvocationTargetException when calling "new Service()" in Axis web service to call another web service Michael Averstegge Java 0 01-10-2006 11:05 PM
Global variables on par with ASP's global.asa Wayne ASP .Net 2 11-11-2003 10:58 PM



Advertisments