10 Einträge, 1 Seite |
1
2
3
4
5
6
package Foo;
use Exporter;
use vars qw/@ISA @EXPORT/;
push @ISA, "Exporter";
push @EXPORT, '$foovar';
$Foo::foovar = 'hallo';
1
2
3
4
5
6
7
8
9
10
11
12
13
14
use strict;
use Foo;
{
local $foovar = "bye"; # Zeile 6
print "$foovar\n"; # prints bye
print "$Foo::foovar\n"; # prints hallo
{
package Bar;
use Foo;
print "$foovar\n"; # prints hallo
}
}
1
2
3
4
5
6
7
8
9
package Foo;
use Exporter;
use vars qw/@ISA @EXPORT/;
@ISA = qw(Exporter);
@EXPORT = qw(*foovar);
$foovar = 'old';
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use Foo;
print "\$foovar\t\t\$Foo::foovar\n";
print "-" x 30,"\n";
print "$foovar\t\t$Foo::$foovar\n";
$foovar = 'new';
print "$foovar\t\t$Foo::$foovar\n";
# Ausgabe:
# $foovar $Foo::foovar
# ------------------------------
# old old
# new new
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]
$>
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;
QuoteKann ich irgendwie sagen, dass ich alle Versionen der Variable, also das original sowie die Importe in allen importierenden Paketen beeinflussen will?
Quote$> perl
CSLocal::value (42);
CSLocal::localize ();
CSLocal::value ("blubber di blah");
print "value is [", CSLocal::value (), "]\n";
CSLocal::restore ();
print "after restore [", CSLocal::value (), "]\n";
value is [blubber di blah]
after restore [42]
$>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package CSLocal;
my $VALUE;
use strict;
my @value_stack = ();
sub undefine_value { undef $VALUE; }
sub value {
$VALUE = $_[0] if @_;
}
sub localize {
push @value_stack, value ();
undefine_value ();
}
sub restore {
die "no values to restore" unless @value_stack;
my $val = pop @value_stack;
value ($val);
}
1;
10 Einträge, 1 Seite |