SPUG: Scope question

Colin Meyer cmeyer at helvella.org
Tue Jun 11 19:06:33 CDT 2002


Hi Peter,

On Tue, Jun 11, 2002 at 04:45:02PM -0700, Peter Darley wrote:
> Colin,
> 	I see what you're pointing to, but I don't understand what it's telling me.
> :)
> 	Does this mean that I can get to my variable using
> Apache::ROOT::MyDirectory::MyScript_2epl->Variable or somesuch?
> 	I'm pretty clueless about Perl namespaces...

That's almost correct, although there's no dereferencing necessary.  

You could access the values like:
  print $Apache::ROOT::MyDirectory::Myscript_2epl::Variable;

Namespaces are simply strings prepended to variable names that
make them unique:

  # they're both called $Variable, relative to their own namespaces
  $MyApp::Variable = $YourApp::Variable;

You can access globals from any namespace by prepending the variable
name with the name of the namespace.  You can access globals in the
current namespace without prepending.  One declares the current namespace
with:
  package Some::Namespace;

The ::'s aren't meaningful, except to the author of the code (i.e. there's
not any explicit inheritence between namespaces).

Rather than using some ugly auto-generated namespace, I'd be inclined
to place application globals in a nicely named namespace:

# in the main script

$MyApp::Variable = 'some value';

# in some module
package Some::Module;

# the full name would be $Some::Module::Variable;
$Variable = 'other value'; 

sub blorq {
  my $it = shift;

  # I want the application-global here:
  $it->{ something } .= $MyApp::Varible;
}


Have fun,
-C.

> Thanks,
> Peter Darley

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list