Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


What are the use strict and use warnings pragmas in Perl?

Arslan Tariq


A pragma is a module that controls some aspect of Perl’s compile or run-time behavior. We’ll discuss two pragmas in this shot:

  • use strict
  • use warning

These pragmas are used in some Perl scripts to avoid confusion for beginners and are often considered training wheels. However, this is a misconception. We find these pragmas in advanced topics of Perl as well, and they are also used by experienced developers.


The use strict pragma

The use strict pragma can be placed at the beginning of a script like this:

use strict;

The use strict pragma forces the developer to code in a manner that enables the code to execute correctly, and it makes the program less error-prone. As an example, the use strict pragma forces the developer to declare variables before using them. We can declare variables with the keyword my in Perl. The keyword my changes the scope of the variable from global to local.

It is a good programming practice to narrow down the variable’s scope, and that’s achievable with the use strict pragma.


If we use the use strict pragma and don’t declare variables, then we will encounter an error. We can see this for ourselves by running the code given below:

use strict;
$z = "Hello!\n";
print $z;
Declaring a variable without the "my" keyword

To avoid this error, we must declare the variable with the my keyword. We can run the program given below and see the string is printed without any problems.

use strict;
my $z = "Hello from the other side!\n";
print $z;
Declaring a variable with the "my" keyword

An unusual case

Everything worked as expected, but now it’s time to look at an unusual case. What if we use the variable $a or $b, instead of $z?

use strict;
$a = "Hello!\n";
$b = "World!\n";
print $a;
print $b;
An unusual case

We can see that the program works completely fine due to $a and $b being special variables in Perl that have global access. We can declare arrays and hashes using the “my” keyword too!

Note: This pragma is implicitly enabled starting with Perl 5.12. This means that if we use Perl 5.12 or any of the later versions, we won’t need to use the use strict statement because the pragma will be enabled by default.

The use warnings pragma

The second pragma we will discuss is the use warnings pragma. This pragma is often paired up with the use strict pragma in programs.

use strict;
use warnings;

The use warnings pragma helps us find any typing errors and warns us when something goes wrong with the program. It can be considered a debugging tool too since it helps the developer find bugs in the program.

The main difference between the two aforementioned pragmas is that the ‘use strict’ pragma will abort the program’s execution if it encounters an error, but the ‘use warning’ pragma will just issue a warning and not abort the program’s execution.

Note: The use warning pragma was introduced in Perl 5.6.




Arslan Tariq
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring