Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

perl

What is the 'Use of uninitialized value' warning in Perl?

Talha Ashar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Perl’s “Use of uninitialized value” warning is a run-time warning encountered when a variable is used before initialization or outside its scope. The warning does not prevent code compilation.

To view possible warnings, you will need to include the warnings library in your program, as shown below:

use warnings;

Warning format

The format of the “Use of uninitialized value” warning is shown below:

Use of uninitialized value %s

The placeholder, %s, represents the variable that was not initialized with a value.

Example

The following example shows how the “Use of uninitialized value” warning arises in Perl:

#!/usr/bin/perl
use warnings;
# declare variable
$name = "Bob";
# print variable
print "Name = $name\n";
print "Age = $age\n";
# intialize value for age
$age = 23;
print "Age = $age\n";

Explanation

First, the variable $name is initialized and output. Since name already has a value, the print statement in line 99 does not cause any problems.

However, the code proceeds to print the variable $age, which has not been initialized. Consequently, the print statement in line 1010 results in the “Use of uninitialized value" warning. The warning alerts the user to the variable’s name that caused the issue, i.e. $age.

Since $age is initialized in line 1414, the final print statement does not prompt any warnings.

RELATED TAGS

perl

CONTRIBUTOR

Talha Ashar
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring