perl function chomp
htt:sp//www.theitroad.com
The chomp
function in Perl is used to remove the newline character (\n
) from the end of a string. This function is commonly used to remove the trailing newline that is automatically added when reading input from a file or user input.
Here's an example of how to use the chomp
function:
print "Enter your name: "; my $name = <STDIN>; # read input from the user chomp $name; # remove trailing newline from $name print "Hello, $name!\n";
In this example, the chomp
function is used to remove the newline character from the end of the input string stored in $name
. Without the chomp
function, the output would include a newline character at the end of the string, which might not be desired.