Perl | undef and the defined function

undef is used for those variables which do not have any assigned value. One can compare it with NULL(in Java, PHP etc.) and Nil(in Ruby). So basically when the programmer will declare a scalar variable and don’t assign a value to it then variable is supposed to be contain undef value. In Perl, if the intial value of a variable is undef then it will print nothing.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Perl program for undef variable 
# variable which declared without 
# initial value
  
# variable x without initial value
my $x;
  
# printing its value
print "The value of x is = ${x}";

chevron_right


Output:



The value of x is = 

undef() function: undef is used to reset the value of any variable. It can be used with or without the parentheses. It means that parentheses are optional while using this function.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Perl program to illustrate 
    # the undef() function
      
    # a variable with initial value 10
    $k = 10;
      
    # displaying its initial value
    print "The value of variable k is ${k}\n";
      
      
    # undef the variable
    undef $k;
      
    # value after the undef function
    print "The value of variable k is ${x}\n";
      
    # a variable with initial value 20
    $m = 20;
      
    # displaying its initial value
    print "The value of variable m is ${m}\n";
      
    # undef the variable
    # you can also use
    # $m = undef; 
    $m = undef(); 
      
    # value after the undef function
    print "The value of variable m is ${m}\n";

    chevron_right

    
    

    Output:

    The value of variable k is 10
    The value of variable k is 
    The value of variable m is 20
    The value of variable m is 
    

defined() function: This function is used to check whether a value is assigned to the variable or not. It will return True if the value is assigned to the variable otherwise it will return false.

  • Syntax:
    defined $variable_name
  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Perl program to illustrate 
    # the defined() function.
      
    # a variable assigned value 15
    $k = 15;
      
    # To check if variable is defined or not
    if (defined $k)
    {
         print "k is defined\n";
    else
    {
        print "k is not defined\n";
    }
      
      
    # undef the variable
    $k = undef;
      
    # To check if the variable is defined or not
    if (defined $k)
    {
        print "k is defined\n";
    else
    {
        print "k is not defined\n";
    }

    chevron_right

    
    

    Output:

    k is defined
    k is not defined
    

Note: Although undefined variables have no defined value, but they can take null value during the operation. For example, if user is adding two variables x and y in which x is 5 and y is undefined, then the result would remain 5 because y will take up the value 0. Similarly, if user concatenates two strings strx and stry with strx as value “GGF” and stry is undef, the result would be “GFG”, as stry takes the value of a blank string “”.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Perl program to demonstrate behaviour 
    # of undef variables, during operation 
    # like airthmetic, concatenation etc.
      
    # first variable assigned with value
    $x = 125;
      
    # second variable with no value
    my $y;
      
    # airthmetic operation
    $z = $x + $y;
      
    # displaying sum
    print "The sum of x and y is ${z}\n";
      
    # declaring strx and assigned a value to it
    $strx = "GFG";
      
    # declaring stry without assigned value
    my $stry;
      
    # string concatenation
    $strz = $strx.$stry;
      
    # after concatenation
    print "After concatenation of strx and stry  ${strz}\n";

    chevron_right

    
    

    Output:

    The sum of x and y is 125
    After concatenation of strx and stry  GFG
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.