linux poison RSS
linux poison Email

Perl Script: How to find if variable is defined or not?

Below is a simple perl script which explains how to find if any given variable is defined or not.

Feel free to copy and use this code.

Source
: cat undefined.pl
#!/usr/bin/perl

use strict;
use warnings;

my $Undefined;
my $Defined = "linuxpoison";

print "The value of Undefined is ", defined $Undefined, "\n";
print "The value of Defined is ", defined $Defined, "\n";

print "------------------------------\n";

$Undefined = $Defined;
$Defined = undef;

print "The value of Undefined is ", defined $Undefined, "\n";
print "The value of Defined is ", defined $Defined, "\n";

Output: perl undefined.pl
The value of Undefined is
The value of Defined is 1
------------------------------
The value of Undefined is 1
The value of Defined is




2 comments:

durga @ unix and linux commands forum said...

Here are your are assigining a value to the variable and using the undef to make the variable empty. I didn't see any code to check for the variables value. You are simply priting the value on the terminal

Unknown said...

Using the out of the

defined $Undefined
defined $Defined

you can compare if the variable is defined or not.

Post a Comment

Related Posts with Thumbnails