Both the functions are quite similar. Both of them remove one character from the end of the given string.

Perl chop()

The Perl chop() function removes last character from a string regardless of what that character is. It returns the chopped character from the string.

Syntax:

  1. chop();  

Perl chop() example

  1. #chop() EXAMPLES    
  2. $a = "AEIOU";    
  3. chop($a);    
  4. print "$a\n";  #it will return AEIO.    
  5. $a = "AEIOU";    
  6. $b = chop($a);    
  7. print "$b\n";  #it will return U.         

Output:

AEIO
U

Look at the output, at first, variable $a is printed after chopping. Then variable $b is printed which returns the chopped character from the string.

Perl chomp()

The chomp() function removes any new line character from the end of the string. It returns number of characters removed from the string.

Syntax:

  1. chomp();  

Perl chomp() example

  1. #chomp() EXAMPLES    
  2. $a = "AEIOU";    
  3. chomp($a);    
  4. print "$a\n";  #it will return AEIOU.   
  5. $a = "AEIOU";    
  6. $b = chomp($a);    
  7.  print "$b\n"; #it will return 0, number .  
  8.  $a = "AEIOU\n";    
  9. chomp($a);    
  10. print "$a\n";  #it will return AEIOU, removing new line character.    
  11. $a = "AEIOU\n";    
  12. $b = chomp($a);    
  13. print "$b\n";  #it will return 1, number of characters removed.  

Output:

AEIOU
0
AEIOU
1

Look at the output, at first, variable $a does not contain any new lone character. Then it is passed in variable $b and printed. It returns 0 as no character is removed.

Now, variable $a contains a new line character. When it is passed in $b, it returns 1 as it removes one new line character.