undef in Perl

undef in the Perl programming language is one of the values that can be contained in a variable. If you do not assign a value to a variable, the variable will be undef.

Here is an example Perl program:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my $var;
print Dumper $var;

$var = 123;
print Dumper $var;

$var = undef;
print Dumper $var;

This program will display:

$VAR1 = undef;
$VAR1 = 123;
$VAR1 = undef;

First we declared a variable but have not assigned it any value: my $var;. Then with the help print Dumper $var; brought information about the value of this variable to the screen (you can see that the variable contains undef). After that we assigned a variable number 123and showed it on the screen. And then using the code $var = undef; we assigned the variable undef and again brought information about it.

undef may be an element in the array or value in hash:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my @arr = (1, undef, 'three');
print Dumper \@arr;

my %h = (
    a => undef,
);
print Dumper \%h;

Often undef is used as a special flag. For example, can be a function that always returns a number, but in case of error returns undef.

Name undef comes from the English word undefined("undefined").

Than undef is not

undef is a single value. This is not a string 'undef', it is not an empty string '' and it's not the number 0. All different values.

▶ Run
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

foreach my $var (undef, 'undef', '', 0) {
    print Dumper $var;
}

In the case of the hash if the key value undef is not the same as the hash is no such key.

How to check on undef

To check whether the value undef function is used defined. The value is undef if the function defined returns false:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;

my $var;

if (!defined($var)) {
    print 'var is undef';
}

It is very easy to write a Saba is_undef, which will return true if it passed undef:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;
use feature qw(say);
use Data::Dumper;

sub is_undef {
    my ($maybe_undef) = @_;
    return !defined($maybe_undef);
}

my @arr = (1, undef, 'three');

foreach my $el (@arr) {
    say is_undef($el) ? 'undef' : 'not undef';
}

But most do not, and use the function defined without any strapping.

There is another way how to check that a variable contains undef is to use the operator ~~ (this operator is called smartmatch). This statement appeared in Perl 5.10, but from Perl 5.18 use of this operator gives a warning Smartmatch is experimental at script.pl line 8.. Here is a sample code with this statement:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;

my $var;

if ($var ~~ undef) {
    print 'var is undef';
}

Often the best way to check for undef is a function defined.

Perl undef and the conversion to JSON

Library for working with JSON in Perl converts Perl value undef in null in JSON:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;
use JSON::PP;

print encode_json {
    a => undef,
};

This code will display {"a":null}.

Conversely, when converting from JSON null becomes undef:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;
use JSON::PP;
use Data::Dumper;

print Dumper decode_json '[null]';

This code will display:

$VAR1 = [
          undef
        ];

Function ref

The result of the function ref with respect to undef is an empty string ''. In this regard undef is not different from strings and numbers. For undef, strings and numbers function ref returns an empty string:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

foreach my $el ('asdf', undef, 100) {
    print Dumper ref($el);
}

A Boolean value

When using undef in logical operations undef is a lie. This code will screen text false:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

if (undef) {
    print 'true';
} else {
    print 'false';
}

Use with strings

In the case if you use undef operations with strings, instead undefwill be use an empty string '', but also shows a warning.

▶ Run
#!/usr/bin/perl

use strict;
use warnings;

my $var;

print 'AAA' . $var . 'BBB';
▶ Run
#!/usr/bin/perl

use strict;
use warnings;

my $var;

if ($var eq '') {
    print 'equal';
} else {
    print 'not equal';
}

In the first case, a warning will be shown Use of uninitialized value in concatenation (.) or string at script.pl line 10., in the second case Use of uninitialized value in string eq at script.pl line 8..

Use with numbers

In the case if you use undef operations with numbers, instead undefwill be used number 0. but the same warning appears.

▶ Run
#!/usr/bin/perl

use strict;
use warnings;

my $var;

print $var + 4;
▶ Run
#!/usr/bin/perl

use strict;
use warnings;

my $var;

if ($var == 0) {
    print 'equal';
} else {
    print 'not equal';
}

In the first case, a warning will be shown Use of uninitialized value $var in addition (+) at script.pl line 8., in the second case Use of uninitialized value $var in numeric eq (==) at script.pl line 8..

It is interesting that when using the increment operator ++ notice no:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;

my $var;

print $var++;

Related topics

Other articles

Comments