chomp is used to eliminate the new line character. It can used in many different scenarios.
For ex: excuteScript.pl firstArgument.
$firstArg = $ARGV[0];
chomp $firstArg; --> to get rid of the carrige return.
Sophia
Aug 14th, 2007
chomp() will remove any erroneous line breaks and spacing from end of the string chomp() will be used whenever input is aquired from users. Example:
#!/usr/local/bin/perl -w print "Enter a number"; #since the user will hit enterkey after entering no ,chomp will remove'n' from $no chomp($no=<>); print "You Entered $no";
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. In fact, by default, chomp only removes what is currently defined as the $INPUT_RECORD_SEPARATOR.
Little correction!!!! 'chomp' is used to eliminate new-line character. -- this is fine, but not the ultimate answer.
Well, 'chomp' can eliminate other characters as well. Actually what happens is, whenever you make a call to 'chomp'; it checks the value of special variable named as $/ Whatever is the value of $/ is eliminated from the scalar on which chomp is invoked.
For example, I want to read a record at a time from a file, and records are separated by %%. The file may look like this:
record_1 %% record_2 %% record_3
Then, my records are separated by "%%n". Isn't it? So, I would code as follows:
The chomp function works in combination with the $/ variable when reading from filehandles.
The $/ variable is the input record separator that is attached to the records you read from a filehandle, and it is by default set to the newline character "n". The chomp function works by removing the last character from a string only if it matches the value of $/. To do a safe strip from a record of the record separator character, just use chomp in place of chop:
chomp VARIABLE
chomp( LIST )
chomp This safer version of "chop" removes any trailing string that corresponds to the current value of $/ (also known as $INPUT_RECORD_SEPARATOR in the "English" module). It returns the total number of characters removed from all its arguments. Its often used to remove the newline from the end of an input record when you're worried that the final record may be missing its newline. When in paragraph mode ("$/ = """), it removes all trailing newlines from the string. When in slurp mode ("$/ = undef") or fixed-length record mode ($/ is a reference to an integer or the like, see perlvar)
chomp() wont remove anything. If VARIABLE is omitted, it chomps $_. Example:
while (<>) {
chomp; # avoid
on last field
@array = split(/:/);
# ...
}
If VARIABLE is a hash, it chomps the hashs values, but not its keys.
You can actually chomp anything thats an lvalue, including an assignment:
chomp($cwd = `pwd`);
chomp($answer = );
If you chomp a list, each element is chomped, and the total number of characters removed is returned.
Note that parentheses are necessary when you're chomping anything that is not a simple variable. This is because "chomp $cwd = `pwd`;" is interpreted as "(chomp $cwd) = `pwd`;", rather than as "chomp( $cwd = `pwd` )" which you might expect. Similarly, "chomp $a, $b" is interpreted as "chomp($a), $b" rather than as "chomp($a, $b)".
babu
Jun 20th, 2012
Hi i am babu.chomp is pre define function it will delete last character if it is a new line character
ex1:$a="babu
";
chomp($a);
print $a;#it will not print new line ...after babu.
ex2:print "Enter a name:";#babu
chomp($name=);
print "Hi $name,welcome";
#o/p:Hi babu welcome in single line if there in no chomp here i will display in 2 line after babu bcz of enter is a new line character.
What is meant by 'chomp'? where do we require this ?