标签:use number Perl strict my example
Perl - use strict
Where to use?
Condition 1: too much lines in your script;
Condition 2: can not find out the reason for error.
Why to use?
Reason 1: help you to find out some easy errors like spelling mistakes; (eg. you claimed ‘apple’ but you used ‘aple’)
Reason 2: force you to minimize the use range of the variables. (It will force you to define the local variable)
How to use?
Step 1: use strict;
Step 2: use ‘my’ to define variable.
Usually error 1: Global symbol “$xxx” requires explicit package name at …
Usually error 2: Server Error - please check ‘error logs’ or use CGI::Carp pakage.
Step 3: some examples.
#!use/bin/perl
use strict;
# example 1
my $string = "hello world";
my @array = qw(ABC DEF);
my %hash = (A=>1, B=>2);
# example 2
foreach my $name (@names){
print "Name: $name\n";
}
# example 3
my $number = 0;
foreach my $digit (@digits)
$number = 10== $number + $digit;
}
print "Number: $number\n";
# example 4
sub my_sub {
my ($arg1, $arg2) = @_;
print "Arg1: $arg1 Arg2: $arg2\n";
}
# example 5
$sth->bind_columns(\my ($field1, $field2));
while ($sth->fetch) {
print "F1: $field1 F2: $field2\n";
}
Extension
What is ‘warnings’?
# In Perl 5.6+
use strict;
use warnings;
# In Perl 5.6- (usually use)
#!usr/bin/perl -w
# other method
# set $^W (if $^w not in BEGIN{}, useless)
$^W = 1;
# or
BEGIN{$^W = 1}
# you can limit the range of $^W
# 1
sub add_two_numbers_which_might_be_undef {
# 参见 'perldoc perllexwarn'
# 因为最好是只在你希望的地方禁止掉warning
no warnings "uninitialized";
$_[0] + $_[1];
}
# 2
sub add_two_numbers_which_might_be_undef {
local $^W;
$_[0] + $_[1];
}
或者,你应像前面例子中声明 '$number’一样初始化变量。
你还可以参阅Ovid的妙文use strict’ is not Perl
以及(Wog指出的):Use strict warnings and diagnostics和Use strict warnings and diagnostics or die.
取自"http://wiki.perlchina.org/index.php/Use_Strict_And_Warnings"
标签:use,number,Perl,strict,my,example 来源: https://blog.csdn.net/ygxdyd/article/details/121532409
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。