Instance code for initialization of Perl constants, multidimensional arrays and variables

Time:2019-8-20

Example 1:

Copy codeThe code is as follows:
#!/usr/bin/perl
use strict;
use warnings;
my $test = “asdf”;
print “${test}_test2\n”;
#constant
use constant {
    AAA => “aaa”,
    BBB=> “bbb”,
    MIN_TOTAL => 12,
    SCORE_PASS => 90,
    SCORE_RED => 70,
};
print AAA;
print SCORE_PASS;
#two dimesion arrays
my @steps = (
      [“aaa”, “aaavalue”],
      [“bbb”,”bbbvalue”],
      [“ccc”,”cccvalue”]
);
print “\n”;
foreach my $i (0 .. $#steps){
  print “$steps[$i][0]:$steps[$i][1]\n”;
}

Code 2:

Copy codeThe code is as follows:
my $a1;
print “$a1\n”;
my $a2 = undef;
print “$a2\n”;
if(!defined($a1)){print “a1 is not defined\n”;}
if(!$a2){print “a2 is not defined\n”;}
my $a3=”;
if(!$a3){print “a3 is empty string\n”;}

When defining variables, it is necessary to initialize them, or to determine whether they are defined or not when they are used. In many cases, it is necessary to determine whether they are empty strings. Especially when using getopt:: long or CGI – > query to get parameters, it is necessary to check whether the parameters are defined or not, and if no definitions are considered, give default values.