Re: private method



"Chas Owens" schreef:

www.nas.nasa.gov/News/Techreports/2000/PDF/nas-00-008.pdf.

Recently I was playing with something similar, when context-variables
were discussed on perl6.language:

#!/usr/bin/perl5 -l
use strict;
use warnings;

sub context {

sub bad_scope { $_[0] .q/->context, bad scope [/. $_[1] .q/]/ }

my $not_init = __PACKAGE__ .q/->context->{init}() not called yet/;
my $bad_init = q/no re-init allowed/;
my ($scope, $value);

{ id => \$scope,

init => sub {
if ($scope) {
$scope eq caller or die bad_scope $scope, caller;
die $bad_init;
}
$scope = caller;
return;
},

set => sub {
$scope or die $not_init;
$scope eq caller or die bad_scope $scope, caller;
$value = $_[0];
return;
},

get => sub {
$scope or die $not_init;
$scope eq caller or die bad_scope $scope, caller;
return $value;
},
};
}

my ($id, $init, $set, $get) = @{ context() }{ qw/id init set get/ };

print "id:$id";
$init->();
print $get->() || q/<undef>/;

$set->('foo');
print $get->();

$set->('bar');
print $get->();


# { no strict 'refs';
# use Data::Dumper;
# print Dumper( \*{'main::'}{HASH} );
# }

print '------';

package Test;

# $set->('baz');
# print $get->();
# print '======';

my ($id1, $init1, $set1, $get1) = @{ main->context() }{ qw/id init set
get/ };

print "id1:$id1";
$init1->();
print $get1->() || q/<undef>/;

$set1->('foo');
print $get1->();

$set1->('bar');
print $get1->();

$set1->('baz');
print $get1->();
print '======';

__END__

My next step was to add locking, but I got busy elsewhere.

See also: Scalar::Readonly, Readonly and Readonly::XS, attributes.

--
Affijn, Ruud

"Gewoon is een tijger."

.



Relevant Pages

  • Re: Finding the name of a variable used in the caller of a function from within the function itself.
    ... Erwin Moller ... A function should be totally stand-alone in scope and whatever it needs from the outside world should be passed into it via the argument list. ... This works for any var in the global scope, so vars inside the function itself, other functions, or classes wouldn't be visible. ... I was merely asking because I was curious if there was a way to find the name of the argument in the caller from within the funtion itself. ...
    (comp.lang.php)
  • Re: Convention for naming of packages
    ... I feel that does rather depend on the name's scope and how many other ... The object parameter to the subprogram should be named well ... of the operation's caller, not from your point of view as ...
    (comp.lang.ada)
  • Re: How to make return value from a bash function call non-zero when there is an error in the fu
    ... There is an error in the function, but the final return value masks it to the caller. ... to be able to do "set -e" inside the function and have its scope be limited ... Someone who can take time out from flashing her ...
    (comp.unix.shell)
  • Re: Testing Delphi DLL crashes VB6 IDE
    ... s goes out of scope before the function returns, and thus the pointer is not valid by the time it arrives at the caller. ... But since the address that s held is probably still within your process's address space, and COM probably hasn't re-allocated it to anything else, it probably still contains the same characters it did a moment ago when it was still a valid pointer, so the caller will blithely read it and get what you were expecting, so it appears to work. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Packages and returning errors
    ... > array intact. ... sub is_a_instance_method { ... my $class = shift; ... You need to fix the scope of $error by moving its declaration outside ...
    (comp.lang.perl.misc)