kal
kal

Reputation: 29401

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

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) {
    ...
}

Upvotes: 39

Views: 62185

Answers (5)

swilliams
swilliams

Reputation: 326

It depends on what you plan on doing with the variable whether or not it is defined; as of Perl 5.10, you can do this (from perl51000delta):

A new operator // (defined-or) has been implemented. The following expression:

 $a // $b

is merely equivalent to

defined $a ? $a : $b

and the statement

$c //= $d;

can now be used instead of

$c = $d unless defined $c;

Upvotes: 3

vol7ron
vol7ron

Reputation: 42139

It depends on what you're trying to do. The proper C way to do things is to initialize variables when they are declared; however, Perl is not C, so one of the following may be what you want:

  1)   $var = "foo" unless defined $var;      # set default after the fact
  2)   $var = defined $var? $var : {...};     # ternary operation
  3)   {...} if !(defined $var);              # another way to write 1)
  4)   $var = $var || "foo";                  # set to $var unless it's falsy, in which case set to 'foo'
  5)   $var ||= "foo";                        # retain value of $var unless it's falsy, in which case set to 'foo' (same as previous line)
  6)   $var = $var // "foo";                  # set to $var unless it's undefined, in which case set to 'foo'
  7)   $var //= "foo";                        # 5.10+ ; retain value of $var unless it's undefined, in which case set to 'foo' (same as previous line)


C way of doing things (not recommended):

# initialize the variable to a default value during declaration
#   then test against that value when you want to see if it's been changed
my $var = "foo";
{...}
if ($var eq "foo"){
   ... # do something
} else {
   ... # do something else
}

Another long-winded way of doing this is to create a class and a flag when the variable's been changed, which is unnecessary.

Upvotes: 15

rafl
rafl

Reputation: 12341

Perl doesn't offer a way to check whether or not a variable has been initialized.

However, scalar variables that haven't been explicitly initialized with some value happen to have the value of undef by default. You are right about defined being the right way to check whether or not a variable has a value of undef.

There's several other ways tho. If you want to assign to the variable if it's undef, which your example code seems to indicate, you could, for example, use perl's defined-or operator:

$var //= 'a default value';

Upvotes: 38

msbmsb
msbmsb

Reputation: 1416

'defined' will return true if a variable has a real value.

As an aside, in a hash, this can be true:

if(exists $h{$e} && !defined $h{$e})

Upvotes: 2

Axeman
Axeman

Reputation: 29854

If you don't care whether or not it's empty, it is. Otherwise you can check

if ( length( $str || '' )) {}

Upvotes: 5

Related Questions