The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

RapidApp::ScopedGlobals

SYNOPSIS

  use RapidApp::ScopedGlobals;
  sub foo_0 {
    RapidApp::ScopedGlobals->applyForSub({ x => 1 }, \&foo_1 );
  }
  
  use RapidApp::ScopedGlobals qw( sEnv );
  sub foo_1 {
    sEnv->applyForSub({ x => 2 }, \&foo_2 );
    print sEnv->x;
  }
  sub foo_2 {
    print sEnv->x, ",";
  }
  
  # calling foo_0 prints "2,1"

DESCRIPTION

This ScopedGlobals package implements a type of "constant environment" global. No code may modify the value of the global for its current scope, but it may redefine a global for the scope of a sub-method (often an anonymous sub).

By using globals this way, they become much more like environment variables in nested shell scripts. It limits "spooky action from a distance" because no routine can modify its parents' variables (though unfortunately it is still possible to modify the deep contents of a hash which could be seen by a parent). Instead, they act much more like invisible parameters to a method, which can help when working with frameworks where it is impossible or just inconvenient to pass those parameters to an inner module or method.

METHODS

sEnv

This is a very simple sugar constant which returns "RapidApp::ScopedGlobals".

It makes it easy to call methods on this package.

applyForSub( \%, \CODE )

Calls the given coderef with the given alterations of the "global" variables. When the coderef returns, the global values will revert to what they were before the call. See perl's "local" keyword for implementation details.

varExists( $varName )

Returns true of the named global has been set by a parent function of the current call stack.

get( $varName )

Returns the value of the named scoped-global, or undef if it has not been set. Note that a scoped-global could have been set to undef. Use "varExists" to determine the difference.

RapidApp::ScopedGlobals defines an auto-loader that allows you to use any named global as if it were a method. If the global has not been defined in this call stack, an exception is thrown. This allows better debugging of typo'd var names, but if you don't want an exception, use "get".

1 POD Error

The following errors were encountered while parsing the POD:

Around line 97:

Unknown directive: =head