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.

Recommended Today

What are the new methods of visual + map technology?

Last week, Ren Xiaofeng, chief scientist of Alibaba Gaode map, made a technical exchange with you on the development of computer vision related technology and the application in the field of map travel at the online live broadcast activity of “cloud dialogue” between senior students of Alibaba. The interaction between live broadcast is hot. Especially […]