Home » 2014 » May » 2 » A very basic program in perl scripting

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:

#!/usr/bin/perl

print "Hello world";


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.

#!/usr/bin/perl

print "Enter your name";

$Name = <STDIN>;

print "You have entered:".  $Name. "\n";

<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.

#!/usr/bin/perl

print "Ente your name";

$name = <STDIN>;

print "Your name is :". $name;

print "Your name in Upper case:". uc $name;

print "First letter of ur name in upper case :". ucfirst $name;

prrint "Length of your name :". length $name;

chop ($name);

print "The last letter removed from name: ". $name;

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;

See File Handling in PERL

We will see more examples and concepts in coming days. :)

 
 

Category: Technical Solution | Views: 1765 | Added by: shanky | Tags: introduction to perl language, firsrt perl program, basics of perl., how to create a program in perl, a basic program in perl | Rating: 0.0/0

Related blogs


You may also like to see:


[2015-02-05][Technical Solution]
Soving Rubik's Cube First layer: Layered approach by Shanky
[2014-05-02][Technical Solution]
A very basic program in perl scripting
[2015-01-26][Technical Solution]
How to change IP address of windows system
[2014-03-09][Technical Solution]
Creating a webcam in Visual basic Programming
[2014-11-28][Technical Solution]
Problem Step Recorder(PSR). How to use it?

Total comments: 2
avatar
0
1 shanky • 10:10 AM, 2014-07-06
demo comment
avatar
0
2 Anonymously • 11:10 AM, 2014-07-06
demo
avatar