RE: chomp operator
- From: rfernandez@xxxxxxxxx (Richard Fernandez)
- Date: Tue, 30 Oct 2007 10:42:17 -0400
Hi,
Hello,
so where does the chomp operator plays its role, can some one
explain me here with a sample of code.
Thanks and Regards
Kaushal
One place where chomp() comes in handy is when you're reading from a
file.
Usually a line read from a file will have a newline at the end.
The terminating new line may get in the way of your processing, here's a
contrived example:
my $foo = 'some text to append to line\n';
open my $TESTFILE, '<', '/some/test/file') or die;
while ( defined (my $line = <TESTFILE>) ) {
# $line contains 'Some arbitrary text \n'
print $line, $foo;
}
Without a 'chomp $line' the program will print:
Some arbitrary text
some text to append to line
Instead of:
Some arbitrary text some text to append to line
as was probably intended.
HTH
richf
.
- References:
- chomp operator
- From: Kaushal Shriyan
- chomp operator
- Prev by Date: Re: chomp operator
- Next by Date: Re: Perl 5.8.6 bug in open()?
- Previous by thread: Re: chomp operator
- Next by thread: Pragmas use strict and use warnings
- Index(es):
Relevant Pages
|