Nano's Blog

01/05/2010

The chomp Operator

Filed under: programming — Tags: — nano @ 10:31
#!C:\Perl\bin\perl.exe
use strict;
use warnings;

my $text = "a line of text\n\n"; # Or the same thing from <STDIN>
chomp($text);   # Gets rid of the newline character, get rid of the newline \n
chomp $text;    # chomp is acturally a function, get the value 1, get rid of another newline \n
print $text;

Output


D:\learning\perl>hello.pl
a line of text
D:\learning\perl>

More test, if we commented one chomp expression

#chomp $text;    # chomp is acturally a function, get the value 1, get rid of another newline \n

Output(include one newline)


D:\learning\perl>hello.pl
a line of text
(# newline)
D:\learning\perl>

More test, and if we commented both of the two chomp expressions

#chomp($text);   # Gets rid of the newline character, get rid of the newline \n
#chomp $text;    # chomp is acturally a function, get the value 1, get rid of another newline \n

Output (include two newlines)

D:\learning\perl>hello.pl
a line of text
(#newline0)
(#newline1)
D:\learning\perl>

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress