Monday, December 27, 2010

chomp(), chop(), substr(), length(), uc(), ucfirst(), lcfirst() functions in Perl

These are some inbuilt functions in Perl which are commonly used in the course 
of programing

#!/usr/bin/perl
print "content-type: text/html \n\n";

$str="hello world\n";

chomp($str);  # removes the new line character from the end of a string
print $str."Intrasoft\n";

chop($str); # removes the last character from the end of a string
print $str."\n";

$temp_str=substr($str,4,3); # returns the characters starting from the 5th position of the string to 3 characters from the 5th position of the string
print $temp_str."\n";

$temp_str=substr($str,4); # returns the characters starting from the 5th position of the string till the end of the string
print $temp_str."\n";

$str_len=length($str); # return the length of the string in number of charaters
print $str_len."\n";

$temp_str=uc($str); # converts and returns the string in upper case
print $temp_str."\n";

$temp_str=ucfirst($str); # converts and returns the string with it's 1st character in upper case
print $temp_str."\n";

$temp_str=lc($str); # converts and returns the string in lower case
print $temp_str."\n";

$temp_str=lcfirst($str); # converts and returns the string with it's 1st character in lower case
print $temp_str."\n";

Output:
-------
hello worldIntrasoft
hello worl
o w
o worl
10
HELLO WORL
Hello worl
hello worl
hello worl