“全局符号需要显式包名"的解释 [英] Explanation of 'global symbol requires explicit package name'

查看:36
本文介绍了“全局符号需要显式包名"的解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习 Perl,每当我编写一个重要的脚本时,我总是收到此错误消息.我一直认为我对它有很好的理解,但我想我没有.这是一个带有以下错误的草率马尔可夫链示例(未经测试).

I've been learning Perl and whenever I write a non-trivial script I always get this error message. I always think I have a good understanding of it, but I suppose I don't. Here is a sloppy markov chain example (not tested) with the errors below.

#!/usr/bin/perl -w

use strict;

sub croak { die "$0: @_: $!\n"; }


sub output {
  my %chains = shift;
  my @keys = keys %chains;

  my $index = rand($keys);
  my $key = $keys[$index];

  my $out_buf = $key;
  for (my $i = 0; $i < 100; ++$i) {
    my $aref = $chains{$key};
    my $word = @$aref[rand($aref)];
    $out_buf .= " $word";

    $key =~ s/.+ //;
    $key .= " $word";
  }
  print $out_buf, "\n";
}


sub get_chains {
  my %chains;
  my @prefixes

  while (my $line = <FILE>) {
    my @words = split " ", $line;

    foreach my $word (@words) {
      if ($prefixes == 2) {
    my $key = join " ", @prefixes;

    my $arr_ref = $chains{$key};
    push(@$arr_ref, $word);

    shift @prefixes;
      }
      push(@prefixes, $word);
    }
  }

  return %chains;
}



sub load_book {
  my $path_name = shift @ARGV; 
  open(FILE, $path_name) || croak "File not found.\n"; 
}

load_book;
my %chains = get_chains;
output %chains;

----ERRORS----

"my" variable $line masks earlier declaration in same statement at markov.pl line 33.
"my" variable $path_name masks earlier declaration in same scope at markov.pl line 55.
Global symbol "$keys" requires explicit package name at markov.pl line 12.
syntax error at markov.pl line 32, near ") {"
Global symbol "$prefixes" requires explicit package name at markov.pl line 36.
Global symbol "%chains" requires explicit package name at markov.pl line 48.
syntax error at markov.pl line 49, near "}"
syntax error at markov.pl line 56, near "}"
Execution of markov.pl aborted due to compilation errors.

我犯了什么错误?

推荐答案

您的脚本中存在三个语法错误:

There are three syntax errors in your script:

全局符号$keys"需要在 markov.pl 第 12 行明确的包名称.

您没有声明 $keys,并且由于使用严格",这是一个致命错误.你可能的意思是:

You didn't declare $keys, and because of "use strict", that is a fatal error. You probably meant:

my $index = rand(@keys);

第二个错误:

全局符号$prefixes"需要在 markov.pl 第 36 行显式包名称.

是同一件事:你的意思是:

is the same thing: you meant:

if (@prefixes == 2) {

最后,在第 30 行,后面缺少一个分号:

Finally, in line 30, you're missing a semicolon after:

my @prefixes

这会混淆解析器,并导致所有其他错误和警告.

This confuses the parser, and causes all the other errors and warnings.

如果您不清楚 sigils 的使用,您可能需要阅读 perldata 文档($, @, %).

You may want to read the perldata documentation if you're unclear about the use of sigils ($, @, %).

这篇关于“全局符号需要显式包名"的解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆