Science, Tech, Math › Computer Science Perl Array chop() and chomp() Function - Quick Tutorial Share Flipboard Email Print Rob Melnychuk/Stockbyte/Getty Images Computer Science Perl PHP Programming Python Java Programming Javascript Programming Delphi Programming C & C++ Programming Ruby Programming Visual Basic View More By Kirk Brown Kirk Brown Computer Science Expert B.A., Abilene Christian University Kirk Brown is a systems software engineer with expertise in Perl, PHP, Ruby, and Python. Learn about our Editorial Process Updated on October 21, 2017 Perl's chop and chomp functions can often be a source of confusion. Not only do they sound similar, they do similar things. Unfortunately, there is a critical difference—chop removes the last character of the string completely, while chomp only removes the last character if it is a newline. Chomping $myName cuts off the last newline, leaving just Jacob. Once it's been chomped, further chomping won't do anything at all. Chopping the name, however, will result in the last character being removed, leaving Jaco: Chomping and chopping an array results each element being acted on, and can be a real time saver. So remember - Chop chops off the last character without question or regret. Chomp only removed the newline, leaving the string itself intact. Chomp does not remove all whitespace characters by default. In fact, by default, chomp only removes what is currently defined as the $INPUT_RECORD_SEPARATOR. If your goal is to trim all whitespace from the end of your string, try using a regex like this one submitted by a reader: Cite this Article Format mla apa chicago Your Citation Brown, Kirk. "Perl Array chop() and chomp() Function - Quick Tutorial." ThoughtCo, Aug. 26, 2020, thoughtco.com/perl-array-chop-chomp-function-tutorial-2641180. Brown, Kirk. (2020, August 26). Perl Array chop() and chomp() Function - Quick Tutorial. Retrieved from https://www.thoughtco.com/perl-array-chop-chomp-function-tutorial-2641180 Brown, Kirk. "Perl Array chop() and chomp() Function - Quick Tutorial." ThoughtCo. https://www.thoughtco.com/perl-array-chop-chomp-function-tutorial-2641180 (accessed May 27, 2023). copy citation