The ’use strict’ and ’use warnings’ pragmas are used in Perl to enforce stricter rules and warnings at compile-time.
’use strict’ enables a set of rules that restricts the use of undeclared variables and subroutines, and prohibits the use of symbolic references. It also enforces other features like scalar variables being declared with ’my’, and it disallows the use of barewords (strings not quoted as strings). This makes the code more consistent, less error-prone, and easier to maintain over time. Here is an example:
use strict;
use warnings;
my $name = "John";
print $Name; # This will throw an error because $Name is not declared.
In this example, ’use strict’ caught a typo where $Name was mistakenly capitalized instead of $name.
’use warnings’ enables runtime warnings, which detect potential issues in code at runtime. For example, it can issue a warning if a value expected to be numeric is used in a non-numeric context, or if a variable is used only once but not initialized. This is useful for debugging and finding potential issues early on. Here is an example:
use warnings;
my $count = "one";
print $count + 1; # This will throw a warning that $count is used in a non-numeric context.
In this example, ’use warnings’ caught that $count was used in a numeric context even though it was a string, which may indicate an unintentional error.