NAME
BATsh::Env - Shared variable store for BATsh
SYNOPSIS
use BATsh::Env;
BATsh::Env::init(); # seed from %ENV
BATsh::Env::set('FOO', 'bar'); # store (key uppercased)
my $v = BATsh::Env::get('foo'); # fetch -- same as 'FOO'
BATsh::Env::setlocal('ENABLEDELAYEDEXPANSION');
# ... commands that use !VAR! ...
BATsh::Env::endlocal();
DESCRIPTION
BATsh::Env is the single variable table shared by BATsh::CMD and BATsh::SH. All variable names are stored in uppercase; all lookups (get, set, unset, exists_var) are case-insensitive, matching cmd.exe behaviour.
Delayed Expansion
The package variable $DELAYED_EXPANSION (default 0) controls whether !VAR! references are expanded by expand_cmd().
setlocal($opts) saves a snapshot of the entire variable store and the current $DELAYED_EXPANSION flag onto an internal scope stack, then optionally updates the flag:
setlocal('ENABLEDELAYEDEXPANSION') -- sets $DELAYED_EXPANSION = 1
setlocal('DISABLEDELAYEDEXPANSION') -- sets $DELAYED_EXPANSION = 0
setlocal('') -- no flag change
endlocal() pops the stack and restores both the variable store and the flag, so nested SETLOCAL/ENDLOCAL pairs each get their own isolated scope.
Variable Expansion
expand_cmd($str) performs three passes:
- 1.
%~[modifiers]Nbatch-parameter tilde expansion (e.g.%~dp0,%~nx1). Supported modifier letters:f(full path),d(drive),p(directory),n(basename without extension),x(extension). With no modifiers, surrounding double-quotes are stripped. - 2.
%VAR%substitution (case-insensitive lookup via uppercase key). Unresolved references expand to the empty string.%%is replaced with a literal%. - 3.
!VAR!substitution (only when$DELAYED_EXPANSIONis true). Unresolved!VAR!references expand to the empty string.
expand_sh($str) expands ${VAR} and $VAR for the SH interpreter. It tries the exact-case key first, then the uppercase key, so variables set by CMD (uppercase keys) are visible in SH sections as $var or $VAR.
FUNCTIONS
init()-
Seeds
%STOREfrom%ENV(keys uppercased) and resets$DELAYED_EXPANSIONto 0. get($name)-
Returns the value of variable
$name(case-insensitive), orundef. set($name, $value)-
Stores
$valueunder the uppercase form of$name. unset($name)-
Deletes the variable.
exists_var($name)-
Returns 1 if the variable is defined, 0 otherwise.
sync_to_env()-
Copies
%STOREto%ENVso child processes spawned viasystem()inherit the current variable state. setlocal($opts)-
Pushes the current store and
$DELAYED_EXPANSIONflag onto the scope stack, then parses$optsfor ENABLEDELAYEDEXPANSION or DISABLEDELAYEDEXPANSION. endlocal()-
Pops and restores the store and flag from the scope stack.
expand_cmd($str)-
Expands
%VAR%(and!VAR!when delayed expansion is active). expand_sh($str)-
Expands
${VAR}and$VARfor the SH interpreter. delayed_expansion()-
Returns the current value of
$DELAYED_EXPANSION(0 or 1).
AUTHOR
INABA Hitoshi <ina@cpan.org>
LICENSE
Same as Perl itself.