开发者

Is there any difference b/w goto &func; and &func; in Perl?

goto &func;

&func;

They two seems identical开发者_如何学C to me after some test,is that the case?


There is a substantial difference. Check here: perl-goto


You were running the wrong tests. Use caller to see what is going on.

#!/usr/bin/perl

use strict;
use warnings;

sub foo {
  my $level = 0;
  while (my $sub = (caller($level))[3]) {
    print "$sub\n";
    ++$level;
  }
  print "\n";
}

sub bar {
  print "sub:\n";
  &foo;
}

sub baz {
  print "goto:\n";
  goto &foo;
}

bar();
baz();

When you run it, you'll see something like:

$  ~/stuff/goto
sub:
main::foo
main::bar

goto:
main::foo
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜