代码之家  ›  专栏  ›  技术社区  ›  Shubham

如何检查变量是否在perl中声明?

  •  8
  • Shubham  · 技术社区  · 16 年前

    我正在使用 use strict;

    unless(defined($x)){
          print "Not defined";
    }
    

    Not defined “但是它返回一个错误

    Global symbol "$x" requires explicit package name at *********** in line 15.
    
    7 回复  |  直到 16 年前
        1
  •  18
  •   Greg Bacon    16 年前

    这个 strict pragma 包含三个部分:严格引用、严格变量和严格sub。你遇到的是

    严格变量

    如果访问未通过声明的变量,则会生成编译时错误 our use vars ,通过 my ,或不完全合格。因为这是为了避免可变的自杀问题和微妙的动态范围界定问题,所以 local 变量不够好。

    因为它会生成编译时错误,所以- BEGIN 代码甚至没有机会运行。您可以临时允许块中的非严格变量,如中所示

    {
      no strict 'vars';
      print "Not defined!\n" unless defined $x;
    }
    

    defined

    告诉我们更多关于你的申请,我们可以给你更好的建议如何处理它。

        2
  •  5
  •   Axeman maxelost    16 年前

    你甚至不能 参考 一个变量,除非它被声明。当你问的时候

    defined( $x ) ?
    

    如果 strict 'vars' use strict --然后它将在包符号表中为“x”创建一个条目。

    有趣的是,如果没有 strict 'refs' 检查包符号表中是否有变量也很容易。

    defined( *{ __PACKAGE__ . '::x' }{SCALAR} )
    

    因为没有办法自动创建词汇表(“我的变量”),所以也没有标准的方法来检查是否声明了词汇表。词汇变量存储在“pad”中。但是有一个模块 PadWalker

    为了检查当前的级别,您可以得到pad的散列,然后检查它是否存在于当前pad中。您还可以通过堆栈进行循环(integer参数的工作原理类似于 caller )找到最近的x在哪里。

    my $h = peek_my (0);
    exists $h->{x};
    
        3
  •  5
  •   Pablo Marin-Garcia    16 年前

    您的问题是“如何检查变量是否在perl中声明”,但随后您要检查变量是否已定义。这是两个不同的概念。

    '使用严格' 您正在自动检查任何变量 未申报 我的 地方的 我们的

    use strict;
    my $x;  # you are missing this part
    [...] | # code
    # your test for define
    print defined $x? "defined\n" : "not defined\n";
    

    请注意,仅为x美元进行的测试对于您的目的是不正确的:

    my ($x,$y, $z);
    $w;         # not declared (use strict will catch it and die)
    $x = 0;     # declared and defined BUT if you make a logic test like 'if ($x) {}' then it will be FALSE, so don't confuse testing for **'$x'** and testing for **'defined $x'**
    $y = undef; # declared but not defined
    $z = 1;     # declared, defined, and logial test TRUE
    

    最后,对我来说,氙杀虫剂的答案似乎是错误的:他建议 “除非$x”这是不正确的 如果定义如我之前所说的。他还建议 “除非存在$x”,这对于测试标量是错误的

        4
  •  1
  •   Michael Pakhantsov    16 年前
     #
     print "Not defined" if !defined($x);
    

    结果将是

     #
     use strict;
     print "Not defined" if !defined($x);
    

    会产生你问题中的错误。

    http://perldoc.perl.org/strict.html ,其中描述了如何仅导入所需的限制。(不过,使用严格的“vars”是一个非常好的主意:)

        5
  •  1
  •   Martijn Pieters    13 年前

    通常情况下,这种代码不应该是一个严肃的程序所必需的,但为什么不只是为了好玩:(假设使用strict)

    print "Not defined\n" unless eval 'ref(\$x)';
    
        6
  •  0
  •   xoid    13 年前
    #!/usr/bin/perl -l
    
    use strict;
    
    # if string below commented out, prints 'lol' , if the string enabled, prints 'eeeeeeeee'
    #my $lol = 'eeeeeeeeeee' ;
    # no errors or warnings at any case, despite of 'strict'
    
    our $lol = eval {$lol} || 'lol' ;
    
    print $lol;
    
        7
  •  0
  •   xoid    13 年前

    我的解决办法是 #!/usr/bin/perl-l文件

    use strict;
    
    # if string below commented out, prints 'lol' , if the string enabled, prints 'eeeeeeeee'
    #my $lol = 'eeeeeeeeeee' ;
    # no errors or warnings at any case, despite of 'strict'
    
    our $lol = eval {$lol} || 'lol' ;
    
    print $lol;
    
        8
  •  -1
  •   costaparas    5 年前

    你可以用 unless

    use 'strict';
    
    my $var = 'defined';
    unless (defined($var)) {
      print "not defined\n";
    }
    
    $var = undef;
    unless (defined($var)) {
      print "not defined\n";
    }
    

    print 不会打印任何东西,第二个会 $var undef .