Thread Variablenscope (7 answers)
Opened by bianca at 2013-01-09 15:34

bianca
 2013-01-09 15:34
#164798 #164798
User since
2009-09-13
6977 Artikel
BenutzerIn

user image
Hallo!
Ich bin gerade etwas überrascht, dass folgender Code kein "use of uninitialized value %test in 13" bringt:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;

my %test = (
    foo => 'bar',
);

my %code = (
    code    => sub {
        my ($ref) = @_;
        $test{foo} = $ref;
    },
);

print Dumper(\%test)."\n";
$code{code}->('bar2');
print Dumper(\%test)."\n";

Wieso muss ich denn die Variable %test nicht in die sub übergeben?
Ist das eine bestimmte Konstellation die zufällig funktioniert oder ist das völlig OK und in Ordnung?
Geht mir in diesem Fall um saubere Programmierung.
Danke
10 print "Hallo"
20 goto 10

View full thread Variablenscope