在启用警告的perl中使用未定义的变量什么时候可以使用?

在启用警告的情况下,perl通常打印使用未初始化的值$foo,如果在表达式中使用$foo,并且尚未分配值,但在某些情况下可以使用该变量,该变量被视为“0”或“没有警告.

在没有警告的情况下可以使用未初始化/未定义的变量的情况如何?

解决方法

概要

>布尔测试
>递增或递减未定义的值
>附加到未定义的值
>自动
>其他mutators

布尔测试

根据perlsyn documentation,

The number 0,the strings '0' and '',the empty list (),and 07001 are all false in a boolean context. All other values are true.

由于未定义的值为false,以下程序

#! /usr/bin/perl

use warnings;

my $var;
print "A\n" if $var;
$var && print "B\n";
$var and print "C\n";
print "D\n" if !$var;
print "E\n" if not $var;
$var or print "F\n";
$var || print "G\n";

输出D到G,没有警告.

递增或递减未定义的值

如果您的代码将递增或递减至少一次,则不需要将标量显式初始化为零:

#! /usr/bin/perl

use warnings;

my $i;
++$i while "aaba" =~ /a/g;
print $i,"\n";

上面的代码输出3,没有警告.

附加到未定义的值

与隐含的零类似,如果你至少要附加一次,就不需要明确地将标量初始化为空字符串:

#! /usr/bin/perl

use warnings;
use strict;

my $str;
for (<*>) {
  $str .= substr $_,1;
}
print $str,"\n";

自动激活

一个例子是“自动化”.从Wikipedia article

Autovivification is a distinguishing feature of the Perl programming language involving the dynamic creation of data structures. Autovivification is the automatic creation of a variable reference when an undefined value is dereferenced. In other words,Perl autovivification allows a programmer to refer to a structured variable,and arbitrary sub-elements of that structured variable,without expressly declaring the existence of the variable and its complete structure beforehand.

例如:

#! /usr/bin/perl

use warnings;

my %foo;
++$foo{bar}{baz}{quux};

use Data::Dumper;
$Data::Dumper::Indent = 1;
print Dumper \%foo;

即使我们没有明确地初始化中间密钥,Perl负责脚手架:

$VAR1 = {
  'bar' => {
    'baz' => {
      'quux' => '1'
    }
  }
};

没有自动化,代码将需要更多的样板:

my %foo;
$foo{bar} = {};
$foo{bar}{baz} = {};
++$foo{bar}{baz}{quux};  # finally!

不要将自动化与其可以产生的未定义值混淆.例如与

#! /usr/bin/perl

use warnings;

my %foo;
print $foo{bar}{baz}{quux},"\n";
use Data::Dumper;
$Data::Dumper::Indent = 1;
print Dumper \%foo;

我们得到

Use of uninitialized value in print at ./prog.pl line 6.

$VAR1 = {
  'bar' => {
    'baz' => {}
  }
};

请注意,中间密钥自动修复.

自动化的其他例子:

>引用数组

my $a;
push @$a => "foo";

>引用标量

my $s;
++$$s;

>引用哈希

my $h;
$h->{foo} = "bar";

可悲的是,Perl没有(还有)自动更新以下内容

my $code;
$code->("Do what I need please!");

其他mutators

In an answer to a similar question,ysth报道

Certain operators deliberately omit the “uninitialized” warning for your convenience because they are commonly used in situations where a 0 or “” default value for the left or only operand makes sense.

These are: ++ and -- (either pre- or post-),+=,-=,.=,|=,^=,&&=,||=.

被定义为“或”// =愉快地突变未定义的值而不发出警告.

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...