📄 dclsym.pm
字号:
package VMS::DCLsym;use Carp;use DynaLoader;use vars qw( @ISA $VERSION );use strict;# Package globals@ISA = ( 'DynaLoader' );$VERSION = '1.03';my(%Locsyms) = ( ':ID' => 'LOCAL' );my(%Gblsyms) = ( ':ID' => 'GLOBAL');my $DoCache = 1;my $Cache_set = 0;#====> OO methodssub new { my($pkg,$type) = @_; 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 methodssub TIEHASH { $_[0]->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 (<P>) { ($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 NAMEVMS::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 DESCRIPTIONThe VMS::DCLsym extension provides access to DCL symbols using atied hash interface. This allows Perl scripts to manipulate symbols ina manner similar to the way in which logical names are manipulated viathe built-in C<%ENV> hash. Alternatively, one can call methods in thispackage directly to read, create, and delete symbols.=head2 Tied hash interfaceThis 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 theelement is that symbol's value. Case is not significant in the key string, asDCL converts symbol names to uppercase, but it is significant in the valuestring. All of the usual operations on associative arrays are supported. Reading an element retrieves the current value of the symbol, assigning to itdefines a new symbol (or overwrites the old value of an existing symbol), anddeleting an element deletes the corresponding symbol. Setting an element toC<undef>, or C<undef>ing it directly, sets the corresponding symbol to the nullstring. You may also read the special keys ':GLOBAL' and ':LOCAL' to find outwhether a default symbol table has been specified for this hash (see C<table>below), or set either or these keys to specify a default symbol table.When you call the C<tie> function to bind an associative array to this package,you may specify as an optional argument the symbol table in which you wish tocreate and delete symbols. If the argument is the string 'GLOBAL', then theglobal symbol table is used; any other string causes the local symbol table tobe used. Note that this argument does not affect attempts to read symbols; ifa symbol with the specified name exists in the local symbol table, it is alwaysreturned in preference to a symbol by the same name in the global symbol table.=head2 Object interfaceAlthough it's less convenient in some ways than the tied hash interface, youcan also call methods directly to manipulate individual symbols. In somecases, this allows you finer control than using a tied hash aggregate. Thefollowing methods are supported:=over 4=item newThis creates a C<VMS::DCLsym> object which can be used as a handle for latermethod calls. The single optional argument specifies the symbol table usedby default in future method calls, in the same way as the optional argument toC<tie> described above.=item getsymIf called in a scalar context, C<getsym> returns the value of the symbol whosename is given as the argument to the call, or C<undef> if no such symbolexists. Symbols in the local symbol table are always used in preference tosymbols in the global symbol table. If called in a list context, C<getsym>returns a two-element list, whose first element is the value of the symbol, andwhose second element is the string 'GLOBAL' or 'LOCAL', indicating the tablefrom which the symbol's value was read.=item setsymThe first two arguments taken by this method are the name of the symbol and thevalue which should be assigned to it. The optional third argument is a stringspecifying the symbol table to be used; 'GLOBAL' specifies the global symboltable, and any other string specifies the local symbol table. If this argumentis omitted, the default symbol table for the object is used. C<setsym> returnsTRUE if successful, and FALSE otherwise.=item delsymThis method deletes the symbol whose name is given as the first argument. Theoptional second argument specifies the symbol table, as described above underC<setsym>. It returns TRUE if the symbol was successfully deleted, and FALSEif it was not.=item clearcacheBecause of the overhead associated with obtaining the list of defined symbolsfor the tied hash iterator, it is only done once, and the list is reused forsubsequent iterations. Changes to symbols made through this package arerecorded, but in the rare event that someone changes the process' symbol tablefrom outside (as is possible using some software from the net), the iteratorwill be out of sync with the symbol table. If you expect this to happen, youcan reset the cache by calling this method. In addition, if you pass a FALSEvalue as the first argument, caching will be disabled. It can be reenabledlater by calling C<clearcache> again with a TRUE value as the first argument.It returns TRUE or FALSE to indicate whether caching was previously enabled ordisabled, respectively.This method is a stopgap until we can incorporate code into this extension totraverse the process' symbol table directly, so it may disappear in a futureversion of this package.=back=head1 AUTHORCharles Bailey bailey@newman.upenn.edu=head1 VERSION1.01 08-Dec-1996=head1 BUGSThe list of symbols for the iterator is assembled by spawning off asubprocess, which can be slow. Ideally, we should just traverse theprocess' symbol table directly from C.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -