How do I check if a Perl scalar variable has been initialized?

Viewed 56832

Is the following the best way to check if a scalar variable is initialized in Perl, using defined?

my $var;

if (cond) {
    $var = "string1";
}

# Is this the correct way?
if (defined $var) {
    ...
}
5 Answers
Related