我是perl的新手(一般编程但我已经习惯了Python)。
use strict;
use warnings;
use diagnostics;
my $simple_variable = "string";
print my $simple_variable;
基本上我想知道为什么这个脚本会返回一个未初始化的值错误,因为该变量已明确定义。
由于
答案 0 :(得分:3)
my
创建一个变量并将其初始化为undef(标量)或空(数组和散列)。它还返回它创建的变量。
因此,
print my $simple_variable;
与
相同my $simple_variable = undef;
print $simple_variable;
你的意思是
my $simple_variable = "string";
print $simple_variable;
我不确定你为什么这么问,因为Perl已经告诉过你了。您的程序输出以下内容:
"my" variable $simple_variable masks earlier declaration in same scope at a.pl
line 6 (#1)
(W misc) A "my", "our" or "state" variable has been redeclared in the
current scope or statement, effectively eliminating all access to the
previous instance. This is almost always a typographical error. Note
that the earlier variable will still exist until the end of the scope
or until all closure referents to it are destroyed.
请注意新声明如何有效消除对先前实例的所有访问权限#34;。