#!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>