2

the perl code is like followed : the problem is that I can not read $key inside sub tweak_server{} ....

my $key;
my %hash = ( flintstones => [ "C:/Users1/f1.xml", "C:/Users1/f2.xml" ], 
            jetsons => [ "C:/Users2/f1.xml" ], 
            simpsons =>    [ "C:/Users3/f1.xml", "C:/Users3/f1.xml", "C:/Users3/f1.xml" ], ); 

foreach $key (keys%hash){
if (scalar@{$hash{$key}}>1){
    foreach my $path (@{$hash{$key}}){
        my $filehandle;
        open($filehandle, "+<$path") or die "cannot open out file out_file:$!";
        my $roots = { TAG => 1 };
        my $handlers = { 'ROOT/TAG' => \&tweak_server,
                    };
        my $twig = new XML::Twig(TwigRoots => $roots,
                             TwigHandlers => $handlers,
                             twig_print_outside_roots => \*$filehandle);
        $twig->parsefile($path);
        say $key;#could read key
        sub tweak_server {
            my ($twig, $root) = @_;
            my $tag2=$root->first_child_text('TAG2'); 
            say $key;# could not read
            if ($tag2=~/$key/){
            #BLABLA
            }
            $twig->flush( $filehandle, pretty_print => 'indented');
        }
    }
}

}

as i state, the $key can be read outside the sub, but not inside..the error appeared:Use of uninitialized value $key

and then i tried a simple situation, just like

my $a="aaa";
open( $filehandle, "+<$path") or die "cannot open out file out_file:$!";
my $roots = { TAG => 1 };
my $handlers = { 'ROOT/TAG' => \&tweak_server,
            };
my $twig = new XML::Twig(TwigRoots => $roots,
                     TwigHandlers => $handlers,
                     twig_print_outside_roots => \*$filehandle
                     );
$twig->parsefile($path);
sub tweak_server {
    say $a;
    my ($twig, $root) = @_;
    my $tags=$root->first_child_text('TAG2');
    my $str="204B";
    if ($tag2=~m/$str/){
    foreach my $b(1...6){
                    say $a;             }
}
$twig->flush( $filehandle, pretty_print => 'indented');

}

in this code, the $a can be read.... i spent one day on this, but still can't fix it...crazy now thank in advance!!

4

2 回答 2

4

您在循环$key之外声明。for然后,在 for 循环中,定义一个关闭的子例程$key

作为一般规则,在最小的适用范围内声明变量。例如:

for my $key (keys ...) {

或者

open my $filehandle, '<', ...

为什么要在循环sub tweak_server体中定义?for在我看来,您想要做的是为每个迭代定义一个新的匿名子。

首先,一个简短的示例复制了您所观察到的内容:

use warnings; use strict;

my $key;
my %hash = qw(a b c d e f);

foreach $key (keys %hash) {
    somesub();
    sub somesub {
        print "$key\n";
    }
}

现在,修复:

use warnings; use strict;

my %hash = qw(a b c d e f);

foreach my $key (keys %hash) {
    my $somesub = sub { print "$key\n" };
    $somesub->();
}

这样,我们在每次迭代时定义一个新的匿名函数,每个新的子函数都会关闭循环变量的每个值。

就您的代码而言,您应该将命名的子替换为

my $tweak_server = sub {
    my ($twig, $root) = @_;
    my $tag2=$root->first_child_text('TAG2'); 
    say $key;# could not read
    if ($tag2=~/$key/){
        #BLABLA
    }
    $twig->flush( $filehandle, pretty_print => 'indented');
}

my $handlers = { 
    'ROOT/TAG' => $tweak_server,
};

或者,更好的是,正如@mirod 所观察到的,传递$keytweak_server

sub tweak_server { 
    my( $key, $twig, $root)= @_;
    ...
}

而且,在循环体中,

my $handlers = { 
    'ROOT/TAG' => sub { tweak_server($key, @_) },
};
于 2011-10-20T17:24:16.113 回答
4

简短版本:如果您在另一个 sub 或循环内部有一个命名的 sub 声明 ( sub foo { ... }),那么您可能做错了什么,您可能想要一个匿名 sub。这里就是这种情况。


sub foo { ... }

基本上是一样的

BEGIN { *foo = sub { ... }; }

如果一个 sub 引用my了它自身之外的任何词法 ( ) 变量,它们将在sub执行时被捕获,这是在编译时使用命名的 subs。一个常见的错误是做

sub outer {
    my ($x) = @_;

    sub inner {
       ... $x ...
    }

   outer();
}

这不起作用,因为my $x多次调用以创建多个变量,但inner只捕获编译时存在的变量。(幸运的是,它发出警告。)

乍一看,您的代码只有一个名为 的变量$key,因此在编译时捕获它应该没有问题。但他们看起来是骗人的。由于 foreach 的别名特性,迭代器变量根本不是它在循环之外的变量。

$ perl -E'
   my $x; say "x: ", 0+\$x;   # Show address of variable.
   my $y; say "y: ", 0+\$y;
   my $z; say "z: ", 0+\$z;
   for $x ($y, $z) { say "i: ", 0+\$x; }
'
x: 155771632
y: 155771584
z: 155771744
i: 155771584
i: 155771744

在你的情况下,你有

$ perl -E'
   my $x; say "x: ", 0+\$x;
   my $y; say "y: ", 0+\$y;
   my $z; say "z: ", 0+\$z;
   for $x ($y, $z) {
      say "i: ", 0+\$x;
      sub f { say "f: ", 0+\$x; }
      f();
   }
'
x: 142992144
y: 142992096
z: 142992256
i: 142992096
f: 142992144
i: 142992256
f: 142992144

您希望 sub$x运行时捕获,这是通过使用匿名 sub 完成的。

$ perl -E'
   my $x; say "x: ", 0+\$x;
   my $y; say "y: ", 0+\$y;
   my $z; say "z: ", 0+\$z;
   for $x ($y, $z) {
      say "i: ", 0+\$x;
      my $f = sub { say "f: ", 0+\$x; };
      $f->();
   }
'
x: 159675200
y: 159675152
z: 159675312
i: 159675152
f: 159675152
i: 159675312
f: 159675312

使固定:

for my $key (keys %hash) {
   if (@{ $hash{$key} } > 1) {
      for my $path (@{ $hash{$key} }) {
         open(my $fh, "+<", $path)             # +< ???
            or die("Can't open \"$path\": $!\n");

         my $tweak_server = sub {
            my ($twig, $root) = @_;
            ...
         };

         my $twig = new XML::Twig->new(
            TwigRoots => { TAG => 1 },
            TwigHandlers => { 'ROOT/TAG' => $tweak_server },
            twig_print_outside_roots => $fh,   # No need for \*$fh
         );
         $twig->parsefile($path);
      }
   }
}

您还可以使用匿名包装器将变量作为参数传递给命名子。

sub tweak_server {
   my ($fh, $key, $twig, $root) = @_;
   ...
}

for my $key (keys %hash) {
   if (@{ $hash{$key} } > 1) {
      for my $path (@{ $hash{$key} }) {
         open(my $fh, "+<", $path)             # +< ???
            or die("Can't open \"$path\": $!\n");

         my $twig = new XML::Twig->new(
            TwigRoots => { TAG => 1 },
            TwigHandlers => {
               'ROOT/TAG' => sub { tweak_server($fh, $key, @_) },
            },
            twig_print_outside_roots => $fh,   # No need for \*$fh
         );
         $twig->parsefile($path);
      }
   }
}
于 2011-10-20T19:33:01.893 回答