1. Perl
  2. Module
  3. here

strict - Strict grammar check

Use the strict module to tighten grammar checks.

use strict;

The strict pragma is a pragma to tighten Perl's grammar checks. Be sure to write the strict pragma when writing Perl scripts. If an undeclared variable is used, it will stop with a compile error when trying to execute the script.

Explanation of strict pragma

Instead of calling it a strict module, we call it a strict pragma, but this is a type of module that changes Perl's compile-time behavior, called a pragma, and it's customary to start with a lowercase letter.

Let's see the effect of the strict pragma. The strict pragma has three effects.

Force variable declaration

The first is the effect of forcing variable declaration . In Perl, variable declared with my are called a lexical variable. If you use the strict pragma, you will not be able to use undeclared variable.

use strict;

# Variable is declared and used, so no error occurs
my $num;
$num = 1
use strict;

# I am using it without declaring it, so an error occurs
$num2 = 2

In the second case, the program exits without running with an error message similar to the following:

Global symbol "$num2" requires explicit package name at a.pl line 4.

Since it is written in English, I will translate it.

The global symbol "$num2" requires an explicit package name. 4th line.

If you see this message, it's almost certain that you're using an undeclared variable. Now that you know the line number, look at that line number and check that the variable name is correct and that you have declared the variable in advance.

Prohibition of unquoted strings

The strict pragma prohibits the use of unquoted strings . In fact, Perl allows you to use unquoted strings if the strict pragma isn't enabled. If you enable the strict pragma, you will not be able to use unquoted strings.

use strict;

# Since the string is quoted, no error occurs
my $str = 'abcde';
use strict;

# An error occurs because the string is quoted
my $str = abcde;

In the second case, the program exits without running with an error message similar to the following:

Bareword "abcde" not allowed while "strict subs" in use at a.pl line 3.

Since it is written in English, I will translate it.

Naked "abcde" is not allowed under the use of "strict subs". 3rd line.
Be sure to enable the strict pragma to avoid such mistakes.

Prohibition of symbolic reference

The third effect is prohibition of symbolic reference . Perl has a feature called symbolic reference, but the strict pragma prohibits the use of symbolic reference.

A symbolic reference is a function that allows you to refer to a symbol (variable or function) simply by a string.

See the script below.

# Addition function
sub total {
  my ($num1, $num2) = @_;
  
  return $num1 + $num2;
}

# Refer to function and execute with symbolic reference
my $num = & {'total'} (1 + 2);

print $num;

There is a function called tatal in this script. Instead of calling "total (1 + 2)", it is called as "& {'total'} (1 + 2)" by referring to the function in the symbolic reference.

If the strict pragma is enabled, the script will exit with an error message similar to the following:

Can't use string ("total") as a subroutine ref while "strict refs" in use at a.pl line 11.

Since it is written in English, I will translate it.

Under the use of "strict refs", the string "total" cannot be used as a subroutine. 11th line.

Occasionally, you may have to use a symbolic reference. For example, when you want to dynamically determine a subroutine or method name. In such a case, you can remove the prohibition of symbolic reference by doing the following.

use strict;

# Remove only the prohibition of symbolic reference
no strict'refs';

Keep in mind that the strict pragma has three effects: "force variable declaration", "prohibit unquoted strings", and "prohibit symbolic reference".

Related Informatrion