Thread Exporter und local (9 answers)
Opened by betterworld at 2003-09-27 20:21

kabel
 2003-09-28 14:11
#57526 #57526
User since
2003-08-04
704 Artikel
BenutzerIn
[default_avatar]
alternativer ansatz mittels CPAN:Class::Singleton:

ist leider nicht so schön, wie perls primitive local zu benutzen, aber naja ...

Quote
$> perl -MCSLocal
my $p = CSLocal->instance ();
$p->value (42);
$p->localize ();
$p->value ("blubber di blah");
print "value is [", $p->value (), "]\n";
$p->restore ();
print "after restore [", $p->value (), "]\n";

value is [blubber di blah]
after restore [42]
$>

CSLocal.pm
[READMORE] # damn it! 8)
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package CSLocal;
use strict;
use base qw/Class::Singleton/;
my @value_stack = ();
sub undefine_value { delete $_[0]->{VALUE}; }
sub value {
my $self = shift;
$self->{VALUE} = $_[0] if @_;
$self->{VALUE};
}
sub localize {
my $self = shift;
push @value_stack, $self->value ();
$self->undefine_value ();
}
sub restore {
my $self = shift;
die "no values to restore" unless @value_stack;
my $val = pop @value_stack;
$self->value ($val);
}
1;

[/READMORE]
-- stefan

View full thread Exporter und local