From the course: Perl 5 Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

The undef function

The undef function - Perl Tutorial

From the course: Perl 5 Essential Training

Start my 1-month free trial

The undef function

- [Voiceover] Sometimes it may be neccessary to remove the value of a variable without setting it to zero, or another value. A lot of languages have a concept of a no value, or some value that's other than what you might normally find in a numerical or string context. In Perl this is called undef, and you use the undef function to accomplish this, so, here's a working copy of undef.pl from chapter 13 of the exercise files, and you notice that there's a variable declared here on line 7, $x, and no value is assigned to it, and so, when I run this, you'll notice it says x is not defined. That's different than x doesn't exist. If I remove this entirely, I just get an error that we haven't even declared x. And so, x exists, and it has a value, and that value is undef. That's the default value, it is initialized with undef, otherwise, it would just have some crazy, random number in it, and so if I give it a value, and run this, it says x is 42, so we're checking the defined state of x there…

Contents