package VMS::DCLsym; use Carp; use DynaLoader; use vars qw( @ISA $VERSION ); use strict; # Package globals @ISA = ( 'DynaLoader' ); $VERSION = '1.05'; my(%Locsyms) = ( ':ID' => 'LOCAL' ); my(%Gblsyms) = ( ':ID' => 'GLOBAL'); my $DoCache = 1; my $Cache_set = 0; #====> OO methods sub new { my($pkg,$type) = @_; $type ||= 'LOCAL'; $type = 'LOCAL' unless $type eq 'GLOBAL'; bless { TYPE => $type }, $pkg; } sub DESTROY { } sub getsym { my($self,$name) = @_; my($val,$table); if (($val,$table) = _getsym($name)) { if ($table eq 'GLOBAL') { $Gblsyms{$name} = $val; } else { $Locsyms{$name} = $val; } } wantarray ? ($val,$table) : $val; } sub setsym { my($self,$name,$val,$table) = @_; $table = $self->{TYPE} unless $table; if (_setsym($name,$val,$table)) { if ($table eq 'GLOBAL') { $Gblsyms{$name} = $val; } else { $Locsyms{$name} = $val; } 1; } else { 0; } } sub delsym { my($self,$name,$table) = @_; $table = $self->{TYPE} unless $table; if (_delsym($name,$table)) { if ($table eq 'GLOBAL') { delete $Gblsyms{$name}; } else { delete $Locsyms{$name}; } 1; } else { 0; } } sub clearcache { my($self,$perm) = @_; my($old); $Cache_set = 0; %Locsyms = ( ':ID' => 'LOCAL'); %Gblsyms = ( ':ID' => 'GLOBAL'); $old = $DoCache; $DoCache = $perm if defined($perm); $old; } #====> TIEHASH methods sub TIEHASH { shift->new(@_); } sub FETCH { my($self,$name) = @_; if ($name eq ':GLOBAL') { $self->{TYPE} eq 'GLOBAL'; } elsif ($name eq ':LOCAL' ) { $self->{TYPE} eq 'LOCAL'; } else { scalar($self->getsym($name)); } } sub STORE { my($self,$name,$val) = @_; if ($name eq ':GLOBAL') { $self->{TYPE} = 'GLOBAL'; } elsif ($name eq ':LOCAL' ) { $self->{TYPE} = 'LOCAL'; } else { $self->setsym($name,$val); } } sub DELETE { my($self,$name) = @_; $self->delsym($name); } sub FIRSTKEY { my($self) = @_; my($name,$eqs,$val); if (!$DoCache || !$Cache_set) { # We should eventually replace this with a C routine which walks the # CLI symbol table directly. If I ever get 'hold of an I&DS manual . . . open(P,'Show Symbol * |'); while (
) {
($name,$eqs,$val) = /^\s+(\S+) (=+) (.+)/
or carp "VMS::DCLsym: unparseable line $_";
$name =~ s#\*##;
$val =~ s/"(.*)"$/$1/ or $val =~ s/^(\S+).*/$1/;
if ($eqs eq '==') { $Gblsyms{$name} = $val; }
else { $Locsyms{$name} = $val; }
}
close P;
$Cache_set = 1;
}
$self ->{IDX} = 0;
$self->{CACHE} = $self->{TYPE} eq 'GLOBAL' ? \%Gblsyms : \%Locsyms;
while (($name,$val) = each(%{$self->{CACHE}}) and !defined($name)) {
if ($self->{CACHE}{':ID'} eq 'GLOBAL') { return undef; }
$self->{CACHE} = \%Gblsyms;
}
$name;
}
sub NEXTKEY {
my($self) = @_;
my($name,$val);
while (($name,$val) = each(%{$self->{CACHE}}) and !defined($name)) {
if ($self->{CACHE}{':ID'} eq 'GLOBAL') { return undef; }
$self->{CACHE} = \%Gblsyms;
}
$name;
}
sub EXISTS { defined($_[0]->FETCH(@_)) ? 1 : 0 }
sub CLEAR { }
bootstrap VMS::DCLsym;
1;
__END__
=head1 NAME
VMS::DCLsym - Perl extension to manipulate DCL symbols
=head1 SYNOPSIS
tie %allsyms, VMS::DCLsym;
tie %cgisyms, VMS::DCLsym, 'GLOBAL';
$handle = new VMS::DCLsym;
$value = $handle->getsym($name);
$handle->setsym($name, $value, 'GLOBAL')
or die "Can't create symbol: $!\n";
$handle->delsym($name, 'LOCAL') or die "Can't delete symbol: $!\n";
$handle->clearcache();
=head1 DESCRIPTION
The VMS::DCLsym extension provides access to DCL symbols using a
tied hash interface. This allows Perl scripts to manipulate symbols in
a manner similar to the way in which logical names are manipulated via
the built-in C<%ENV> hash. Alternatively, one can call methods in this
package directly to read, create, and delete symbols.
=head2 Tied hash interface
This interface lets you treat the DCL symbol table as a Perl associative array,
in which the key of each element is the symbol name, and the value of the
element is that symbol's value. Case is not significant in the key string, as
DCL converts symbol names to uppercase, but it is significant in the value
string. All of the usual operations on associative arrays are supported.
Reading an element retrieves the current value of the symbol, assigning to it
defines a new symbol (or overwrites the old value of an existing symbol), and
deleting an element deletes the corresponding symbol. Setting an element to
C
below), or set either or these keys to specify a default symbol table.
When you call the C