Perl constant, multidimensional array and variable initialization example code


Example 1:

Copy codeThe code is as follows:

use strict;
use warnings;
my $test = “asdf”;
print “${test}_test2\n”;
use constant {
    AAA => “aaa”,
    BBB=> “bbb”,
    MIN_TOTAL => 12,
    SCORE_PASS => 90,
    SCORE_RED => 70,
print AAA;
#two dimesion arrays
my @steps = (
      [“aaa”, “aaavalue”],
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, you must initialize them, or judge whether they are defined when using them. In many cases, you also need to judge whether they are empty strings. In particular, after using getopt:: long or CGI – > query to obtain parameters, check whether they are defined. If there is no definition, consider giving default values.