8:11 PM A very basic program in perl scripting | |||
Perl (Practical extraction and report languages) Perl is a language optimized for scanning arbitrary text files, extracting information from those text files, and printing reports based on that information. It's also a good language for many system management tasks. The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal). See File handling in PERL(Open, read, write) Here is a very basic program in PERL. The program just diplays a string on console:
Compilation: To compile the program, use perl -c file.perl This command will throw compilation error, if any. Execution: ./file.perl Make sure that the user has enough permissions to execute the file. If not, you need to add execute permissions, chmod +x file.perl The program will simply print "Hello world" on the terminal. The first line in Perl program is always a She-Bang Line. It is pointing to the perl interpreter which is used to interrpret the program. The the interpreter perl is always present in /usr/bin/perl or in /usr/bin directory and it is installed by default on a GNU/Linux system print is a common function to display strings to console. Any sequence of characters given in single quotes ' or double quotes " is taken as string in PERL. Remember every statement in perl is ended by ";" The last line may not have a ";". Lets see one more basic example which will accept some input and print it.
<STDIN> is used to accept input from user . Here the input accepted from user is stored in variable $Name which is then displayed on the next line. Notice that "." is a concatenation operator which is used to concatenate two strings. We will see one more example performing string operations on strings.
uc: It will bring the strings in Upper case lc: It will convert the string in lower case lcfirsst: It will make the first letter of string to lower case ucfirst: It will make the first letter of string to upper case lenght: It will return the length of string. chop : It will chop the last letter of the string. Eg. shankars--> shankar chomp: It will remove "\n" from the input string usually taken using <STDIN> Some more string operation in PERL reverse: To reverse the given string. Eg : $str= "shankar"; print "\nReverse of $str:". reverse($str); The ouput will be: raknahs substr : To get the substring of a given string. Eg: $str="shankarkumar"; print "\n Substring:". substr($str,4); # this will print from the 4th character of string till the end. print "\n Substring:". substr($str,-4); # this will print the last 4 characters of the string. print "\n Substring:". substr($str,4,3); # will extract 3 characters starting from 4th index. index/rindex: Used to return index of a subsrting from a string. $temp= index($str, "an"); # This will return the position of first occurance of "an" in the given string. print "\n". $temp; $temp= index($str, "an",10); # This will return the position of first occurance of "an" in string starting from 10th character. If there is no such substring, it will return -1. print "\n". $temp; $temp=rindex($str, "an"); # This will return the position of last occurance of "an" in the given string. print "\n". $temp; $temp= rindex($str, "an",10); # This will return the position of last occurance of "an" in string from the end till the 10th character from start. If there is no such substring, it will return -1. print "\n". $temp; We will see more examples and concepts in coming days. :) | |||
|
Related blogs
You may also like to see:
[2014-12-28] | [Technical Solution] |
How to create ODBC data source and drivers to connect to MS Access database |
[2014-01-20] | [Technical Solution] |
How to start your samsung phone in recovery mode |
[2014-01-08] | [Technical Solution] |
How to shutdown a computer using your Mobile |
[2014-01-24] | [Technical Solution] |
How to create a mobile version of your website |
[2014-11-16] | [Technical Solution] |
What is webmaster tool? What it is used for? |
Total comments: 2 | |
| |